在线文档教程
C++
线程支持 | Thread support

std::packaged_task::make_ready_at_thread_exit

STD::包装[医]任务::使[医]准备好了[医]在[医]螺纹[医]出口

void make_ready_at_thread_exit( ArgTypes... args (since C++11)

调用已转发的存储任务args作为争论。任务或其引发的任何异常的返回值存储在*this...

的所有对象和当前线程退出之后,共享状态才会就绪。线程局部存储时间被销毁。

参数

args-the parameters to pass on invocation of the stored task

返回值

%280%29

例外

std::future_error在下列错误条件下:

  • 存储的任务已经被调用。错误类别设置为promise_already_satisfied...

  • *this没有共享状态。错误类别设置为no_state...

二次

#include <future> #include <iostream> #include <chrono> #include <thread> #include <functional> #include <utility> void worker(std::future<void>& output) { std::packaged_task<void(bool&)> my_task{ [](bool& done) { done=true; } }; auto result = my_task.get_future( bool done = false; my_task.make_ready_at_thread_exit(done // execute task right away std::cout << "worker: done = " << std::boolalpha << done << std::endl; auto status = result.wait_for(std::chrono::seconds(0) if (status == std::future_status::timeout) std::cout << "worker: result is not ready yet" << std::endl; output = std::move(result } int main() { std::future<void> result; std::thread{worker, std::ref(result)}.join( auto status = result.wait_for(std::chrono::seconds(0) if (status == std::future_status::ready) std::cout << "main: result is ready" << std::endl; }

二次

产出:

二次

worker: done = true worker: result is not ready yet main: result is ready

二次

另见

operator()executes the function (public member function)

© cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

http://en.cpPreference.com/w/cpp/线程/Package[医]任务/制作[医]准备好了[医]在[医]螺纹[医]出口