Thanks for quick reply.
Now I understand that you can only set value once for a promise. How can we modify the existing example, that which ever worker thread has finished and called the  set_value(), the next set_value() call gets ignored. One solution  (plz correct me here if wrong)  could be to wrap both set_value() with try catch block. Is there any other solution to get a final result from multiple or thread pools and once the result is obtained, discarding further calls to set_value() ?

On Mon, May 9, 2011 at 9:26 PM, Anthony Williams <anthony.ajw@gmail.com> wrote:
Kazz <sfx810@googlemail.com> writes:

> 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"

That exception is thrown if you call set_value a second time on the same
promise.

> boost::promise<int> pt;    // holds final calculated result
>
> void worker1()
> {
>     pt.set_value(1);
> }
>
> void worker2()
> {
>     pt.set_value(2);
> }

Both workers set the same promise. One of them will therefore throw, if
they both get to the set_value. An uncaught exception in a thread will
terminate the application.

> 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;
> }

Both threads are detached here, and then the process exits. If
the second thread to call set_value is still sleeping, so hasn't yet got
to the set_value call, it will be terminated normally as part of process
exit. If the second thread gets to the set_value call before the process
exits it will abort the process due to the unhandled exception.

Anthony
--
Author of C++ Concurrency in Action     http://www.stdthread.co.uk/book/
just::thread C++0x thread library             http://www.stdthread.co.uk
Just Software Solutions Ltd       http://www.justsoftwaresolutions.co.uk
15 Carrallack Mews, St Just, Cornwall, TR19 7UL, UK. Company No. 5478976

_______________________________________________
Boost-users mailing list
Boost-users@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users