AI写作智能体 自主规划任务,支持联网查询和网页读取,多模态高效创作各类分析报告、商业计划、营销方案、教学内容等。 广告
## 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 ```