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

std::promise

Defined in header <future>

?

?

template< class R > class promise;

(1)

(since C++11)

template< class R > class promise<R&>;

(2)

(since C++11)

template<> class promise<void>;

(3)

(since C++11)

1%29碱基模板

2%29非空专门化,用于在线程之间通信对象。

3%29空专门化,用于通信无状态事件

类模板std::promise提供一个工具来存储稍后通过std::future对象创建的std::promise对象。

每个承诺都与共享状态,其中包含一些状态信息和结果该值可能尚未评估、计算为值%28(可能为空%29)或计算为异常。在共享状态下,承诺可以做三件事:

  • 做好准备承诺将结果或异常存储在共享状态中。标记状态就绪,并取消阻塞等待与共享状态关联的未来的任何线程。
  • 释放承诺放弃对共享状态的引用。如果这是最后一次引用,则共享状态将被销毁。除非这是由std::async该操作尚未准备好,此操作不会阻塞。
  • 抛弃*承诺存储类型的例外std::future_error错误码std::future_errc::broken_promise,使共享状态准备好了,然后释放它。

承诺是承诺未来通信通道的“推送”端:在共享状态下存储值的操作。同步性中定义的28名ASstd::memory_order%29从等待共享状态%28的任何函数成功返回,如std::future::get29%。对同一共享状态的并发访问可能在其他方面发生冲突:例如,std::shared_future::get必须全部为只读或提供外部同步。

成员函数

(constructor)

constructs the promise object (public member function)

(destructor)

destructs the promise object (public member function)

operator=

assigns the shared state (public member function)

swap

swaps two promise objects (public member function)

得到结果

弄到[医]未来返回与承诺结果%28公共成员函数%29相关联的未来

设置结果

集[医]值将结果设置为特定值%28公共成员函数%29。

集[医]价值[医]在[医]螺纹[医]Exit将结果设置为特定值,同时仅在线程退出%28公共成员函数%29处传递通知。

集[医]异常设置结果以指示异常%28公共成员函数%29

集[医]例外[医]在[医]螺纹[医]Exit将结果设置为指示异常,同时仅在线程退出%28公共成员函数%29上传递通知。

非会员职能

std::swap(std::promise) (C++11)

specializes the std::swap algorithm (function template)

帮助者类

std::uses_allocator<std::promise> (C++11)

specializes the std::uses_allocator type trait (class template specialization)

这个例子显示了如何promise<int>可用作线程之间的信号。

二次

代码语言:javascript
复制
#include <vector>
#include <thread>
#include <future>
#include <numeric>
#include <iostream>
#include <chrono>
 
void accumulate(std::vector<int>::iterator first,
                std::vector<int>::iterator last,
                std::promise<int> accumulate_promise)
{
    int sum = std::accumulate(first, last, 0);
    accumulate_promise.set_value(sum);  // Notify future
}
 
void do_work(std::promise<void> barrier)
{
    std::this_thread::sleep_for(std::chrono::seconds(1));
    barrier.set_value();
}
 
int main()
{
    // Demonstrate using promise<int> to transmit a result between threads.
    std::vector<int> numbers = { 1, 2, 3, 4, 5, 6 };
    std::promise<int> accumulate_promise;
    std::future<int> accumulate_future = accumulate_promise.get_future();
    std::thread work_thread(accumulate, numbers.begin(), numbers.end(),
                            std::move(accumulate_promise));
    accumulate_future.wait();  // wait for result
    std::cout << "result=" << accumulate_future.get() << '\n';
    work_thread.join();  // wait for thread completion
 
    // Demonstrate using promise<void> to signal state between threads.
    std::promise<void> barrier;
    std::future<void> barrier_future = barrier.get_future();
    std::thread new_work_thread(do_work, std::move(barrier));
    barrier_future.wait();
    new_work_thread.join();
}

二次

产出:

二次

代码语言:javascript
复制
result=21

二次

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

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

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com