类模板std::promise提供一种设施用以存储一个值或一个异常,之后通过std::promise对象所创建的std::future对象异步获得。注意std::promise只应当使用一次。 每个承诺体都与一个共享状态 关联,其中含有一些状态信息和一个结果,它可能尚未求值,已求值为一个值(可能为void),或者求值为一个异常。承诺可以对共享状...
promise( const promise& other ) = delete; (4) (C++11 起) 构造promise 对象。 1) 默认构造函数。构造具有空共享状态的承诺体;2) 构造具有空共享状态的承诺体。由 alloc 分配共享状态。Alloc 必须满足分配器 (Allocator) 的要求;3) 移动构造函数。使用移动语义从 other 的共享状态构造承诺体对象。构造完毕...
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 ...
这两个promise的类型将在下文分析。 当co_await promise.initial_suspend() 恢复时,开始协程的执行。 总结成伪代码就是: task resuming_on_new_thread(std::jthread& out) { coroutine_state *state = new state(); state.copy(out); auto& promise = state.promise(); auto ret = promise.get_return_...
代码与运行结果展示 主要从 main 函数开始分析,调用 resuming_on_new_thread(out) 启动协程,此时协程内部语句暂未执行 总结伪代码为:co_await 协程执行至遇到 co_await expr,执行相关操作 回顾 co_await promise.initial_suspend(),initial_suspend 返回 awaitable,即 std::suspend_never 或 std:...
To cancel a task, you can use the executor_promise<T>::cancel() API. This API lets you to code and design for cancellations, a highly ignored part of software development. Please keep in mind that if the execution is already started or finished, the API may not be able to cancel the...
(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.wait();f3.wait();std::cout<<"...
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的操作表现类似。在更新承...
共享状态已存储值或异常。设置错误码为 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_...