I want to use futures in a multithreaded C++ application. The value associated to a certain promise should be read by more than one thread. I though boost::shared_future is meant to be used for that, but unfortunately I can read the value only once.
How can I read the value multiple times?
boost::promise<std::string> dataProm;
boost::shared_future<std::string> sharedFut1( boost::move(dataProm.get_future()) );
boost::shared_future<std::string> sharedFut2( sharedFut1 );
dataProm.set_value( std::string("Hello World") ); // (in the producer thread)
std::cout << "Test1: " << sharedFut1.get() << std::endl; // (in consumer thread 1), result: "Test1: Hello World"
std::cout << "Test2: " << sharedFut2.get() << std::endl; // (in consumer thread 2), result: "Test2: "
std::cout << "Test3: " << sharedFut1.get() << std::endl; // (in consumer thread 1, again), result: "Test3: "
(To support older compilers (MSVC...) I want to use Boost instead of the new C++11 stuff.)
Thanks in advance!