std::packaged_task

来自cppreference.com

 
 
线程的支持库
主题
Original:
Threads
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
thread(C++11)
this_thread命名空间
Original:
this_thread namespace
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
get_id(C++11)
yield(C++11)
sleep_for(C++11)
sleep_until(C++11)
相互排斥
Original:
Mutual exclusion
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
mutex(C++11)
timed_mutex(C++11)
recursive_mutex(C++11)
recursive_timed_mutex(C++11)
通用锁管理
Original:
Generic lock management
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
lock_guard(C++11)
unique_lock(C++11)
defer_lock_t
try_to_lock_t
adopt_lock_t
(C++11)
(C++11)
(C++11)
lock(C++11)
try_lock(C++11)
defer_lock
try_to_lock
adopt_lock
(C++11)
(C++11)
(C++11)
条件变量
Original:
Condition variables
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
condition_variable(C++11)
condition_variable_any(C++11)
notify_all_at_thread_exit(C++11)
cv_status(C++11)
期货
Original:
Futures
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
promise(C++11)
future(C++11)
shared_future(C++11)
packaged_task(C++11)
async(C++11)
launch(C++11)
future_status(C++11)
future_error(C++11)
future_category(C++11)
future_errc(C++11)
 
std::packaged_task
成员函数
Original:
Member functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
packaged_task::packaged_task
packaged_task::~packaged_task
packaged_task::operator=
packaged_task::valid
packaged_task::swap
获得的结果
Original:
Getting the result
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
packaged_task::get_future
执行
Original:
Execution
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
packaged_task::operator()
packaged_task::make_ready_at_thread_exit
packaged_task::reset
 
Defined in header <future>
template< class > class packaged_task; //not defined
(1) (C++11 起)
template< class R, class Args... >
class packaged_task<R(Args...)>;
(2) (C++11 起)
。类模板std::packaged_task包装任何可调用的目标(功能,lambda表达式,绑定表达式,或其他功能对象),以便它可以异步调用,它的返回值或抛出的异常是存储在共享的状态,这可以通过std::future一个对象.
Original:
The class template std::packaged_task wraps any callable target (function, lambda expression, bind expression, or another function object) so that it can be invoked asynchronously, and its return value or exception thrown is stored in the shared state, which can be accessed through std::future objects.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
。就像std::functionstd::packaged_task是一个多态,分配器知道容器:存储调用的目标可能被分配在堆上或提供的分配器.
Original:
Just like std::function, std::packaged_task is a polymorphic, allocator-aware container: the stored callable target may be allocated on heap or with a provided allocator.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

目录

[编辑] 。成员函数。

构造的任务对象
Original:
constructs the task object
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(公共成员函数) [edit]
解构的任务对象
Original:
destructs the task object
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(公共成员函数) [edit]
移动的任务对象
Original:
moves the task object
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(公共成员函数) [edit]
任务对象的检查,如果有一个有效的功能
Original:
checks if the task object has a valid function
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(公共成员函数) [edit]
交换两个任务的对象
Original:
swaps two task objects
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(公共成员函数) [edit]
。获得的结果。
Original:
Getting the result
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
返回一个std::future与所承诺的结果
Original:
returns a std::future associated with the promised result
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(公共成员函数) [edit]
。执行。
Original:
Execution
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
执行相应的功能
Original:
executes the function
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(公共成员函数) [edit]
执行的结果是只有一次准备退出当前线程的功能
Original:
executes the function ensuring that the result is ready only once the current thread exits
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(公共成员函数) [edit]
复位放弃以前执行的任何存储结果的状态
Original:
resets the state abandoning any stored results of previous executions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(公共成员函数) [edit]

[编辑] 。非成员函数。

专业的std::swap算法
Original:
specializes the std::swap algorithm
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(函数模板) [edit]

[编辑] 。 Helper类。

专业的std::uses_allocator型特征
Original:
specializes the std::uses_allocator type trait
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(类模板专业化) [edit]

[编辑] 。为例。

#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';
}

Output:

Waiting...Done!
Result is 7

[编辑] 。另请参阅。

(C++11)
等待一个值,该值被设置异步
Original:
waits for a value that is set asynchronously
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(类模板) [edit]