Joe C
Joe C

Reputation: 2827

Use boost::async w/o c++11

I have to use gcc 4.8.3 w/o C++11. Without C++11, boost 1.56 async and future work differently. I guess boost::async returns boost::unique_future by default. W/ C++11, unique_future can be moved to shared_future. So I can do

#include <boost/thread.hpp>
#include <boost/thread/future.hpp>
void do() {}
boost::shared_future<void> f = boost::async(do);
std::vector<boost::shared_future<void>> fs;
fs.push_back(f);

W/o C++11, what macros do we need to make the same code? I guess we need to call boost 'move' explicitly? Otherwise, the returned future cannot be 'casted' to a shared future.

#define BOOST_THREAD_USES_MOVE
#include <boost/thread.hpp>
#include <boost/thread/future.hpp>
boost::unique_future<void> f = boost::async(boost::launch::any, do);

But I am not sure how to 'move' f to an std::vector. Do we need more macros to make this work?

Thank you,

Upvotes: 1

Views: 216

Answers (1)

ecatmur
ecatmur

Reputation: 157364

You can explicitly share a future using the share() member function (not sure when it was added, but it's definitely in Boost.Thread 1.56):

boost::shared_future<void> f = boost::async(do).share();

Shared futures are copyable so you don't need to use move semantics. If you do decide to use move semantics under C++03, you would have to use Boost.Move-aware containers i.e. replace std::vector with boost::container::vector:

boost::unique_future<void> f = boost::async(do);
boost::container::vector<boost::unique_future<void>> fs;
fs.push_back(boost::move(f));

Upvotes: 2

Related Questions