一,带返回值的普通函数充当线程处理函数
int returnValue()
{
return 100;
}
如上述代码段 returnValue() 充当线程处理函数、获取它返回值。
1,采用async:启动一个一异步任务(创建线程,并且执行线程处理函数),返回值future对象
2,通过future对像中的get()方法获取线程返回值
3、如下:
thread t1(returnValue);
t1.join();
future<int> result = async(returnValue);
cout<<result.get()<<endl;
二、带返回值的类成员函数充当线程处理函数
class MM
{
public:
int mmThread(int num)
{
cout<<"MMthread_ID:"<<this_thread::get_id()<<endl;
unm *= 2;
chrono::milliseconds duration(5000); //延时5000
this_thread::sleep_for(duration);
return num;
}
};
对于带返回值的类成员函数充当线程处理函数、如何获取返回值、又可以进行传叁,构建一个对象 。
如下:(对于其他用法、程序里有解释)
void testAsync()
{
MM mm;
auto temp = async(&MM::mmThread,&mm,120);
cout<<temp.get()<<endl;
//async的其他叁数
//launch::async :创建线程,执行线程处理函数
//launch::deferred :线程处理函数延时到,调用wait和get方法时候才会执行,本质是没有创建子线程
auto testDerred = async(launch::deferred,&MM::mmThrad,&mm,100);
cout<<testDerred.get()<<endl;
auto testAsyncParam = async(launch::async,&MM::mmThrad,&mm,100);
}
三、用thread处理带返回值的线程处理函数
3.1,通过类模板 packaged_task 包装线程处理函数
3.2,通过packaged_task 的对象调用get_future获取future对像,再通过get()方法得到子线程处理函数的返回值
如下:
void testPackaged_task()
{
//1,打包普通函数
packaged_task<int(void)> packOne(returnValue);
thread testOne(ref(packOne));
testOne,join();
cout<<taskOne.get_future().get()<<endl;
//2,打包类的成员函数
MM mm;
packaged_task<int(int)> tasKTow(bind(&MM::mmThread,&mm,Placehoders::_1));
thread testTow(ref(taskTow),20);
tesTow.join();
cout<<testTow.get_future().get()<<endl;
//3,打包Lanbda表达式
packaged_task<int(int)> resulthree([](int num)
{
cout<<"thread_id"<<this_thread::get_id()<<endl;
num *= 10;
return num;
}) ;
thread testTree(ref(resulthree),7);
resulthree.join();
cout<<testTree.get_future().get()<<endl;
}
四、promise 获取线程处理函数“返回值”
1,通过promise类模板构造建对象,通过调用set_value存储函数需要返回的值
2,通过get_future获取future对象,再通过get()方法获取线程处理函数中的值
如下:
void testPromiseThread(promise<int> & temp,int date)
{
cout<<"线程ID"<<this_thread::get_id()<<endl;
date *= 3;
temp.set_value(date);
}
void testPromiseTohread(promise<int> &temp)
{
int num = temp.get_future().get();
cout<<num<<endl;
}
void testPromise()
{
promise<int> temp;
thread testp(testPromiseThread,ref(temp),18);
testp.join();
cout<<temp.get_future().get()<<endl;
//temp.set_value(120);
promise<int> date;
date.set_value(120);
auto num = date.get_future();
thread test(testPromiseToThread,ref(num));
test.join();
}
五、完整案例
如下:
#include<thread>
#include<ctime>
#include<chrono>
#include<iostream>
#include<time>
#include<iomanip>
#include<future>
using namespace std;
using namespace this_thread;
//1,带返回值的普通函数充当线程处理函数
int returnValue()
{
return 100;
}
//2,带返回值的类成员函数充当线程处理函数
class MM
{
public:
int mmThread(int num)
{
cout<<"MMthread_ID:"<<this_thread::get_id()<<endl;
unm *= 2;
chrono::milliseconds duration(5000); //延时5000
this_thread::sleep_for(duration);
return num;
}
};
void testAsync()
{
future<int> result = async(testTheradOne);
cout<<result.get()<<endl;
MM mm;
auto temp = async(&MM::mmThread,&mm,120);
cout<<temp.get()<<endl;
auto testDerred = async(launch::deferred,&MM::mmThrad,&mm,100);
cout<<testDerred.get()<<endl;
auto testAsyncParam = async(launch::async,&MM::mmThrad,&mm,100);
}
//3,用thread处理带返回值的线程处理函数
void testPackaged_task()
{
//1,打包普通函数
packaged_task<int(void)> packOne(returnValue);
thread testOne(ref(packOne));
testOne,join();
cout<<taskOne.get_future().get()<<endl;
//2,打包类的成员函数
MM mm;
packaged_task<int(int)> tasKTow(bind(&MM::mmThread,&mm,Placehoders::_1));
thread testTow(ref(taskTow),20);
tesTow.join();
cout<<testTow.get_future().get()<<endl;
//3,打包Lanbda表达式
packaged_task<int(int)> resulthree([](int num)
{
cout<<"thread_id"<<this_thread::get_id()<<endl;
num *= 10;
return num;
}) ;
thread testTree(ref(resulthree),7);
resulthree.join();
cout<<testTree.get_future().get()<<endl;
}
//4,promise 获取线程处理函数“返回值”
void testPromiseThread(promise<int> & temp,int date)
{
cout<<"线程ID"<<this_thread::get_id()<<endl;
date *= 3;
temp.set_value(date);
}
void testPromiseTohread(promise<int> &temp)
{
int num = temp.get_future().get();
cout<<num<<endl;
}
void testPromise()
{
promise<int> temp;
thread testp(testPromiseThread,ref(temp),18);
testp.join();
cout<<temp.get_future().get()<<endl;
//temp.set_value(120);
promise<int> date;
date.set_value(120);
auto num = date.get_future();
thread test(testPromiseToThread,ref(num));
test.join();
}
int main()
{
thread t1(returnValue);
t1.join();
future<int> result = async(returnValue);
cout<<result.get()<<endl;
testAsync();
return 0;
}