Boost.future: are these two pieces of code equivalent?

I am worried about the hidden locking issues that may arise from these two parts of the code. For me, the first works, the second does not. I feel more confident that something like non-working code does not block, but it does not work correctly.

My question is: are these two parts of the code equivalent or the fact that I have .get() in the first continuation for working code can make my code locked at this 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(); }); 

Inoperative 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(); }); 
+1
source share
2 answers

It seems that an error in the implementation of boost.future prevents non-working code from working:

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

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

I am not sure about the exact difference with non-working code, although I know this is correct.

0
source

As you seem to have common problems with using continuations, let me show you what I think is a regular model and demonstrate it live:

Live on coliru

 #define BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION #define BOOST_THREAD_PROVIDES_FUTURE #include <boost/thread/future.hpp> #include <boost/thread.hpp> #include <boost/bind.hpp> using boost::async; using boost::future; int foo(int i) { return i*2; } int main() { auto consequence = async(boost::bind(foo, 42)) .then([&](future<int> j) { return j.get()-4; }) .then([&](future<int> k) { return k.get()/2; }) .then([&](future<int> l) { return l.get()+2; }); return consequence.get(); } 

With an exit 42

0
source

Source: https://habr.com/ru/post/1269209/


All Articles