boost.future:是这两个code相当于件? [英] boost.future: are these two pieces of code equivalent?

查看:319
本文介绍了boost.future:是这两个code相当于件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我担心可能来自这两个code片出现的隐藏的阻塞问题。对我来说,第一个是工作,第二个不是。我觉得更有信心像非工作code不会阻止,但它不能正常工作。

我的问题是:这两个code相当于件或事实,我有一个获得()在工作$ C第一延续$ C可以使我的code。在这一点上阻止?

工作code。

 自动完成= user2.ReceiveChatMessages(),然后([安培]。
(::提振未来<的std ::矢量<&ServerReply GT;>&放大器;&安培; receivedMessages)
    {
        number_received_messages_in_continuation = receivedMessages.get()的大小()。        //将这个不用彷徨让我的code座在任何情况下?
        返回user2.ReceiveChatMessages()获得()。
    })
   。然后([安培](升压::未来<的std ::矢量<&ServerReply GT;>&放大器;&安培; receivedMessages){
       number_received_messages_in_continuation + = receivedMessages.get()的大小()。
});

非工作code。

 自动完成= user2.ReceiveChatMessages(),然后([安培]。
(::提振未来<的std ::矢量<&ServerReply GT;>&放大器;&安培; receivedMessages)
    {
        number_received_messages_in_continuation = receivedMessages.get()的大小()。        //没有获得()这里
        返回user2.ReceiveChatMessages();
    })。解开()//我们需要解开
   。然后([安培](升压::未来<的std ::矢量<&ServerReply GT;>&放大器;&安培; receivedMessages){
       number_received_messages_in_continuation + = receivedMessages.get()的大小()。
});


解决方案

这似乎是在 boost.future 什么prevents非执行中的错误从工作的工作code:

https://svn.boost.org/trac/boost/ticket/10964

在code在功能上等同,但工作code是在收益user2.ReceiveChatMessages的阻塞()。获得(),所以如果延续在调用它的线程中执行,这可能是阻塞的。

我不知道有关与非工作code准确的区别,虽然我知道这是正确的。

I am concerned about hidden blocking issues that could arise from these two pieces of code. For me, the first one is working, the second one is not. I feel more confident about something like the non-working code wouldn't block, but it is not working correctly.

My question is: are those two pieces of code equivalent or the fact that I have a .get() in the first continuation for the working code can make my code blocking at that point?

Working code.

auto finished = user2.ReceiveChatMessages().then([&]
(boost::future<std::vector<ServerReply>> && receivedMessages) 
    {
        number_received_messages_in_continuation = receivedMessages.get().size();

        //Will this .get make my code block in any case?
        return user2.ReceiveChatMessages().get();
    })
   .then([&](boost::future<std::vector<ServerReply>> && receivedMessages) {
       number_received_messages_in_continuation += receivedMessages.get().size();
});

Non-working code.

auto finished = user2.ReceiveChatMessages().then([&]
(boost::future<std::vector<ServerReply>> && receivedMessages) 
    {
        number_received_messages_in_continuation = receivedMessages.get().size();

        //No .get() here
        return user2.ReceiveChatMessages();
    }).unwrap() //We need to unwrap
   .then([&](boost::future<std::vector<ServerReply>> && receivedMessages) {
       number_received_messages_in_continuation += receivedMessages.get().size();
});

解决方案

It seems to be a bug in the implementation of boost.future what prevents non-working code from working:

https://svn.boost.org/trac/boost/ticket/10964

The code is functionally equivalent but working code is potentially blocking in the return user2.ReceiveChatMessages().get(), so if the continuation is executed in the thread that invokes it, it is potentially blocking.

I am not sure about the exact difference with the non-working code, though I know that it is correct.

这篇关于boost.future:是这两个code相当于件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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