[问题] std::promise的疑问

楼主: Clangpp (Clang++)   2018-01-27 14:41:21
小弟我在研究C++11的std::promise
找到这篇文章
http://thispointer.com/c11-multithreading-part-8-stdfuture-stdpromise-and-returning-values-from-thread/
然后根据他的范例code操作
范例code如下
#include <iostream>
#include <thread>
#include <future>
void initiazer(std::promise<int> * promObj)
{
std::cout<<"Inside Thread"<<std::endl; promObj->set_value(35);
}
int main()
{
std::promise<int> promiseObj;
std::future<int> futureObj = promiseObj.get_future();
std::thread th(initiazer, &promiseObj);
std::cout<<futureObj.get()<<std::endl;
th.join();
return 0;
}
但是在单步执行产生了疑问,为什么在th建构阶段时就可以让future取得value了??
我一直以为要到join的阶段
恳请各位版友解惑
谢谢
作者: steve1012 (steve)   2018-01-27 16:07:00
有设定值以后就可以读了 你是想要问哪部分你期待的output是啥?future::get是blocking call http://en.cppreference.com/w/cpp/thread/future/get网址没贴好 在这里 https://goo.gl/cH4g2A

Links booklink

Contact Us: admin [ a t ] ucptt.com