std::packaged_task
这个函数是要跟std::future联合使用的。在异步编程中得到未来值
template<typename FunctionType>
void foo(FunctionType f)
{
// return type is the type of the result when invoking functiontype with no params
using return_type = typename std::result_of<FunctionType()>::type;
// package_task 不接受拷贝构造,而是移动构造,这主要是为了保持他的单例性
std::packaged_task<FunctionType> task(std::move(f));
std::future<return_type> result(task.get_future());
//return result; // need to change void to return type
}

这篇博客探讨了C++中std::packaged_task和std::future在异步编程中的应用。std::packaged_task用于创建可异步执行的任务,它不接受拷贝构造而采用移动构造以保持任务的唯一性。通过std::future,可以获取任务执行的结果。示例代码展示了如何创建并执行一个异步任务,以及如何获取返回值。
873

被折叠的 条评论
为什么被折叠?



