#include <thread>
#include <future>
using namespace std;
void test(promise<int> &promiseObj)
{
cout<<"test"<<endl;
promiseObj.set_value(100); //设置函数返回值
}
void main()
{
promise<int> promiseObj;
thread threadObj;
threadObj = thread(test,ref(promiseObj)); //ref(promiseObj)为执行函数test的入参
threadObj.join() //主线程等待子线程结束。
/**
threadObj.detach() //主线程与子线程分离,主线程不等待子线程结束。
**/
int count = promiseObj.get_future().get();//从子线程中获取返回值
}
评论区