Hi All. I am trying to use Futures, but not getting the expected result. The code below works some times, and other times it throws exception
"terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::promise_already_satisfied> >'
what(): Promise already satisfied"
Any help will be appreciated.
Thanks Kazz.
//-------------------------------------------//
boost::promise<int> pt; // holds final calculated result
void worker1()
{
// do any processing using algorithm A
srand ( time ( NULL) );
int delay = rand() % 5 + 1;
boost::this_thread::sleep( boost::posix_time::seconds(delay));
// result calculate
pt.set_value(1);
}
void worker2()
{
// do any processing using algorithm B
srand ( time ( NULL) );
int delay = rand() % 5 + 1;
boost::this_thread::sleep( boost::posix_time::seconds(delay));
// result calculated
pt.set_value(2);
}
int main() {
boost::shared_future<int> ans(pt.get_future());
boost::thread th1(worker1);
boost::thread th2(worker2);
ans.wait();
std::cout<<ans.get()<<std::endl;
std::cout<<"exiting"<<std::endl;
return 0;
}