首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::promise::set_value_at_thread_exit

void set_value_at_thread_exit( const R& value );

(1)

(member only of generic promise template)(since C++11)

void set_value_at_thread_exit( R&& value );

(2)

(member only of generic promise template)(since C++11)

void set_value_at_thread_exit( R& value );

(3)

(member only of promise<R&> template specialization)(since C++11)

void set_value_at_thread_exit()

(4)

(member only of promise<void> template specialization)(since C++11)

存储value进入共享状态,而不使状态立即就绪。当当前线程退出时,状态已经就绪,在所有具有线程本地存储持续时间的变量都已被销毁之后。

这个操作的行为就像set_value,,,set_exception,,,set_value_at_thread_exit,和set_exception_at_thread_exit在更新允诺对象时获取与承诺对象关联的单个互斥对象。

如果没有共享状态,或者共享状态已经存储了值或异常,则引发异常。

参数

value

-

value to store in the shared state

返回值

%280%29

例外

std::future_error在下列条件下:

  • *this没有共享状态。错误类别设置为no_state...
  • 共享状态已存储值或异常。错误类别设置为promise_already_satisfied...

此外:

的副本构造函数引发的任何异常。value

3%29移动构造函数引发的任何异常。value

二次

代码语言:javascript
复制
#include <iostream>
#include <future>
#include <thread>
 
int main()
{
    using namespace std::chrono_literals;
    std::promise<int> p;
    std::future<int> f = p.get_future();
    std::thread([&p] {
          std::this_thread::sleep_for(1s);
          p.set_value_at_thread_exit(9);
    }).detach();
 
    std::cout << "Waiting..." << std::flush;
    f.wait();
    std::cout << "Done!\nResult is: " << f.get() << '\n';
}

二次

产出:

二次

代码语言:javascript
复制
Waiting...Done!
Result is: 9

二次

另见

set_value

sets the result to specific value (public member function)

代码语言:txt
复制
 ? cppreference.com

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

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com