C++ 期货与承诺
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12620186/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Futures vs. Promises
提问by ?imon Tóth
I'm confusing myself with difference between a future and a promise.
我对未来和承诺之间的区别感到困惑。
Obviously, they have different methods and stuff, but what is the actual use case?
显然,它们有不同的方法和东西,但实际用例是什么?
Is it?:
是吗?:
- when I'm managing some async task, I use future to get the value "in future"
- when I'm the async task, I use promise as the return type to allow the user get a future from my promise
- 当我管理一些异步任务时,我使用未来来获得“未来”的价值
- 当我是异步任务时,我使用 promise 作为返回类型,以允许用户从我的 promise 中获得未来
回答by ronag
Future and Promise are the two separate sides of an asynchronous operation.
Future 和 Promise 是异步操作的两个不同方面。
std::promise
is used by the "producer/writer" of the asynchronous operation.
std::promise
由异步操作的“生产者/编写者”使用。
std::future
is used by the "consumer/reader" of the asynchronous operation.
std::future
由异步操作的“消费者/读者”使用。
The reason it is separated into these two separate "interfaces" is to hidethe "write/set" functionality from the "consumer/reader".
将其分成这两个独立的“接口”的原因是对“消费者/阅读器”隐藏“写入/设置”功能。
auto promise = std::promise<std::string>();
auto producer = std::thread([&]
{
promise.set_value("Hello World");
});
auto future = promise.get_future();
auto consumer = std::thread([&]
{
std::cout << future.get();
});
producer.join();
consumer.join();
One (incomplete) way to implement std::async using std::promise could be:
使用 std::promise 实现 std::async 的一种(不完整)方法可能是:
template<typename F>
auto async(F&& func) -> std::future<decltype(func())>
{
typedef decltype(func()) result_type;
auto promise = std::promise<result_type>();
auto future = promise.get_future();
std::thread(std::bind([=](std::promise<result_type>& promise)
{
try
{
promise.set_value(func()); // Note: Will not work with std::promise<void>. Needs some meta-template programming which is out of scope for this question.
}
catch(...)
{
promise.set_exception(std::current_exception());
}
}, std::move(promise))).detach();
return std::move(future);
}
Using std::packaged_task
which is a helper (i.e. it basically does what we were doing above) around std::promise
you could do the following which is more complete and possibly faster:
使用std::packaged_task
which is a helper(即它基本上做我们上面做的事情)std::promise
你可以做以下更完整和可能更快的操作:
template<typename F>
auto async(F&& func) -> std::future<decltype(func())>
{
auto task = std::packaged_task<decltype(func())()>(std::forward<F>(func));
auto future = task.get_future();
std::thread(std::move(task)).detach();
return std::move(future);
}
Note that this is slightly different from std::async
where the returned std::future
will when destructed actually block until the thread is finished.
请注意,这std::async
与std::future
销毁时返回的will 实际阻塞直到线程完成的位置略有不同。