|
Boost Users : |
Subject: [Boost-users] [boost::thread] using futures
From: Kazz (sfx810_at_[hidden])
Date: 2011-05-09 16:10:50
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;
}
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net