#include <iostream>
#include <thread>
#include <chrono>
#include <future>
using namespace std;
//用async
int returnval()
{
return 100;
}
void test01()
{
future<int> ret=async(returnval);
cout << ret.get();
}
//打包
void test02()
{
packaged_task<int(void)> p1(returnval);
thread t2(ref(p1));
t2.join();
cout << p1.get_future().get() << endl;
}
//promise
void pros(promise<int>& temp)
{
temp.set_value(100);
}
int main()
{
promise<int> temp;
thread t(pros, ref(temp));
t.join();
cout << temp.get_future().get() << endl;
return 0;
}
C++Thread-带返回值的线程处理函数
最新推荐文章于 2023-10-02 11:30:00 发布