MCPcopy Create free account
hub / github.com/changkun/modern-cpp-tutorial / foo2

Function foo2

code/7/7.1.cpp:16–26  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

14}
15
16void foo2() {
17 // 将一个返回值为7的 lambda 表达式封装到 task 中
18 // std::packaged_task 的模板参数为要封装函数的类型
19 std::packaged_task<int()> task([](){return 7;});
20 // 获得 task 的 future
21 std::future<int> result = task.get_future(); // 在一个线程中执行 task
22 std::thread(std::move(task)).detach(); std::cout << "Waiting...";
23 result.wait();
24 // 输出执行结果
25 std::cout << "Done!" << std:: endl << "Result is " << result.get() << '\n';
26}
27
28int main() {
29 std::thread t(foo);

Callers 1

mainFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected