侧边栏壁纸
博主头像
银河驿站博主等级

行动起来,活在当下

  • 累计撰写 85 篇文章
  • 累计创建 17 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录
C++

C++11 多线程操作

Administrator
2020-04-10 / 0 评论 / 0 点赞 / 3854 阅读 / 749 字
#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();//从子线程中获取返回值
}
0

评论区