- #include
- #include
- #include
- #include
- voidshow(std::future<int>& fut) {
- std::cout <<"work thread:"<< std::this_thread::get_id() << std::endl;
- intx = fut.get();// 等待获取到结果std::cout << x << std::endl;
- }
- int main(){
- std::cout <<"main thread:"<< std::this_thread::get_id() << std::endl;
- std::promise<int> promise;
- std::future<int> future = promise.get_future();
- std::thread work_thread(show, std::ref(future));
- std::this_thread::sleep_for(std::chrono::seconds(3));
- promise.set_value(10);
- work_thread.join();
- return 0;
- }
来源: