类模板std::packaged_task包装任何可调用(Callable)目标(函数、lambda 表达式、bind 表达式或其他函数对象),使得能异步调用它。其返回值或所抛异常被存储于能通过std::future对象访问的共享状态中。 正如std::function,std::packaged_task是多态、具分配器的容器:可在堆上或以提供的分配器分配其所存储的可调用目标。
Equivalent to*this=packaged_task(std::move(f)), wherefis the stored task. Parameters (none) Return value (none) Exceptions std::future_errorif*thishas no shared state. The error condition is set tono_state. std::bad_allocif there was not enough memory for a new shared state. ...
C++ 并发支持库 std::packaged_task void operator()( ArgTypes... args ); (C++11 起) 如同以 INVOKE<R>(f, args...) 调用存储的任务 f。任务返回值或任何抛出的异常被存储于共享状态。令共享状态就绪,并解除阻塞任何等待此操作的线程。 参数
packaged_task::make_ready_at_thread_exit packaged_task::reset Non-member functions swap(std::packaged_task) Helper classes uses_allocator<std::packaged_task> (until C++17) Deduction guides(C++17)template< class R, class Alloc > struct uses_allocator<std::packaged_task<R>, Alloc> : true_ty...
(http://en.cppreference.com/w/cpp/thread/packaged_task): <urlopen error [Errno 101] Network is unreachable> URL failed (http://en.cppreference.com/w/cpp/thread/promise): <urlopen error [Errno 101] Network is unreachable> URL failed (http://en.cppreference.com/w/cpp/thread/sleep_for)...
#include <iostream>#include <future>#include <thread>intmain(){// future from a packaged_taskstd::packaged_task<int()>task([](){return7;});// wrap the functionstd::future<int>f1=task.get_future();// get a futurestd::thread(std::move(task)).detach();// launch on a thread//...
void swap( packaged_task<Function(Args...)> &lhs, packaged_task<Function(Args...)> &rhs ) noexcept; (since C++11) Specializes the std::swap algorithm for std::packaged_task. Exchanges the state of lhs with that of rhs. Effectively calls lhs.swap(rhs). Parameters lhs, rhs - pack...
std::swap(std::packaged_task) (C++11) 特化std::swap算法 (函数模板) std::swap(std::optional) (C++17) 特化std::swap算法 (函数模板) std::swap(std::any) (C++17) 特化std::swap算法 (函数) std::swap(std::variant) (C++17) 特化std::swap算法 ...
packaged_task::valid packaged_task::swap Getting the result packaged_task::get_future Execution packaged_task::operator() packaged_task::make_ready_at_thread_exit packaged_task::reset Non-member functions swap(std::packaged_task) Helper classes uses_allocator<std::packaged_task> (until C++17) ...
packaged_task (并发 TS) 使用std::experimental::future的std::packaged_task修改版本 (类模板) when_all (并发 TS) 产生在所有给定furure或shared_future均就绪时成为就绪的future (函数模板) when_any (并发 TS) 产生在至少一个给定 future 或 shared_future 就绪时成为就绪的 future ...