一,带返回值的普通函数充当线程处理函数
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,&