c++ - 此示例中 cppreference 中的任务是什么?

标签 c++ c++11 future packaged-task

this 为例从 cppreference 的 packaged_task 描述中,出现一个名为 task 的类。它是什么?

#include <iostream>
#include <future>
#include <thread>

int main()
{
    std::packaged_task<int()> task([](){return 7;}); // wrap the function
    std::future<int> result = task.get_future();  // get a future
    std::thread(std::move(task)).detach(); // launch on a thread
    std::cout << "Waiting...";
    result.wait();
    std::cout << "Done!\nResult is " << result.get() << '\n';
}

最佳答案

taskstd::packaged_task<int()> 类型的对象。它是在第一行创建的。

关于c++ - 此示例中 cppreference 中的任务是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12221136/

相关文章:

C++使用promise,future从线程返回多个值?

java - 在 Callable 中等待回调

c++ - 打印到控制台时,字符之间会出现额外的符号

c++ - 用户定义的文字中是否允许使用 C++14 位分隔符?

c++ - 返回对象数组的函数

C++ 模板部分特化 - 最特化的是 unique_ptr<t>

C++整数类型自动64位

c++ - BJAM,为 vc-80 而不是 vc7.1 构建

c++ - C++11 中的模板约束

firebase - 按下按钮时如何显示与 FutureBuilder 的对话框?