类模板std::promise提供一种设施用以存储一个值或一个异常,之后通过std::promise对象所创建的std::future对象异步获得。注意std::promise只应当使用一次。 每个承诺体都与一个共享状态 关联,其中含有一些状态信息和一个结果,它可能尚未求值,已求值为一个值(可能为void),或者求值为一个异常。承诺可以对共享状态做三件事
std::promise<R&>特化 voidset_value(R&value); (3)(C++11 起) std::promise<void>特化 voidset_value(); (4)(C++11 起) 1-3)原子地存储value到共享状态,并使状态就绪。 4)使状态就绪。 set_value、set_exception、set_value_at_thread_exit和set_exception_at_thread_exit的操作表现类似。在更新承...
std::promise<R>::promise C++ Concurrency support library std::promise promise(); (1)(since C++11) template<classAlloc> promise(std::allocator_arg_t,constAlloc&alloc); (2)(since C++11) promise(promise&&other)noexcept; (3)(since C++11) ...
std::promise<R>::set_value Main template voidset_value(constR&value); (1)(since C++11) voidset_value(R&&value); (2)(since C++11) std::promise<R&>specializations voidset_value(R&value); (3)(since C++11) std::promise<void>specialization ...
结合上面的分析 我们再回顾一下co_await promise.initial_suspend(); initial_suspend的返回值就是awaitable, 同时没有重载 即是awaiter std::suspend_always的关键信息是constexpr bool await_ready() const noexcept { return false; } std::suspend_never自然相反constexpr bool await_ready() const noexcept {...
代码与运行结果展示 主要从 main 函数开始分析,调用 resuming_on_new_thread(out) 启动协程,此时协程内部语句暂未执行 总结伪代码为:co_await 协程执行至遇到 co_await expr,执行相关操作 回顾 co_await promise.initial_suspend(),initial_suspend 返回 awaitable,即 std::suspend_never 或 std:...
get(); // Connects to the cluster std::cout << "Started the Hazelcast C++ client instance " << hz.get_name() << std::endl; // Prints client instance name return 0; }This should print logs about the cluster members and information about the client itself such as client type and ...
()std::future<int>f2=std::async(std::launch::async,[](){return8;});// future from a promisestd::promise<int>p;std::future<int>f3=p.get_future();std::thread([](std::promise<int>&p){p.set_value(9);},std::ref(p)).detach();std::cout<<"Waiting...";f1.wait();f2....
共享状态已存储值或异常。设置错误码为 promise_already_satisfied。 示例运行此代码 #include <future> #include <iostream> #include <thread> int main() { std::promise<int> p; std::future<int> f = p.get_future(); std::thread t([&p] { try { // 可能抛出的代码 throw std::runtime_...
std::promise<R>::get_futureC++ 并发支持库 std::promise std::future<R> get_future(); (C++11 起) 返回与 *this 关联同一状态的未来体对象。 若*this 无共享状态或已调用 get_future,则抛出异常。可使用 std::future::share 以获取承诺体-未来体交流通道的多个“弹出”端。