期货与承诺 [英] Futures vs. Promises

查看:136
本文介绍了期货与承诺的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



显然,他们有不同的方法和东西,但是实际的用例是什么? / p>

是吗?:




  • 当我管理一些异步任务时,未来使用未来获取值

  • 当我是异步任务时,我使用promise作为返回类型,以允许用户从我的承诺中获得未来


解决方案

Future和Promise是异步操作的两个独立方面。



std :: promise 由异步操作的生产者/作者使用。



std :: future 由异步操作的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();

使用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()); //注意:不能使用std :: promise< void>需要一些元模板编程超出了这个问题的范围
}
catch(...)
{
promise.set_exception(std :: current_exception());
}
},std :: move ();

return std :: move(future);
}

使用 std :: packaged_task 这是一个帮助(即它基本上做我们上面做的) std :: promise 你可以做以下更完整,可能更快: p>

  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))。

return std :: move(future);
}

请注意,这与 std :: async 其中返回的 std :: future 将在被销毁时实际阻塞,直到线程完成。


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

解决方案

Future and Promise are the two separate sides of an asynchronous operation.

std::promise is used by the "producer/writer" of the asynchronous operation.

std::future is used by the "consumer/reader" of the asynchronous operation.

The reason it is separated into these two separate "interfaces" is to hide the "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:

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:

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.

这篇关于期货与承诺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆