NIUCLOUD是一款SaaS管理后台框架多应用插件+云编译。上千名开发者、服务商正在积极拥抱开发者生态。欢迎开发者们免费入驻。一起助力发展! 广告
## std::promise ```cpp struct A { template <typename R> void thread_set_promise(int a, std::promise<R>& promiseObj) { std::cout << "In child thread, wait...\n"; std::this_thread::sleep_for(std::chrono::milliseconds(1000)); promiseObj.set_value(35); std::cout << "finished\n"; } void test() { std::promise<int> promiseObj; std::future<int> futureObj = promiseObj.get_future(); int a; // 参数为引用的药用std::ref std::thread t(&A::thread_set_promise<int>, this, a, std::ref(promiseObj)); std::cout << futureObj.get() << std::endl; t.join(); } }; int main() { A a; a.test(); return 0; } ``` 输出结果: ``` In child thread, wait... finished 35 ```