c++11获取线程返回值

#include    //std::cout std::endl
#include      //std::thread
#include      //std::future std::promise
#include     //std::ref
#include      //std::chrono::seconds

void initiazer(std::promise &promiseObj) {
	std::cout << "Inside thread: " << std::this_thread::get_id() << std::endl;
	std::this_thread::sleep_for(std::chrono::seconds(1));
	promiseObj.set_value(35);
}

int main() {
	std::promise promiseObj;
	std::future futureObj = promiseObj.get_future();
	std::thread th(initiazer, std::ref(promiseObj));

	std::cout << futureObj.get() << std::endl;

	th.join();
	return 0;
}

你可能感兴趣的:(C/C++)