开发者

How to get the exception reported to boost::future?

开发者 https://www.devze.com 2023-03-20 20:04 出处:网络
If I use Boost futures, and the future reports true to has_exception(), is there any way to retrieve that exception? For example, here is the following code:

If I use Boost futures, and the future reports true to has_exception(), is there any way to retrieve that exception? For example, here is the following code:

int do_something() {
    ...
    throw some_exception();
    ...  
}

...

boost::packaged_task task(do_something);
boost::unique_future<int> fi=task.get_future();
boost::thread thread(boost::move(task));
fi.wait(开发者_如何学Python);
if (fi.has_exception()) {
    boost::rethrow_exception(?????);
}
...

The question is, what should be put in the place of "?????"?


According to http://groups.google.com/group/boost-list/browse_thread/thread/1340bf8190eec9d9?fwc=2, you need to do this instead:

#include <boost/throw_exception.hpp>

int do_something() {
    ...
    BOOST_THROW_EXCEPTION(some_exception());
    ...  
}

...
try
{
  boost::packaged_task task(do_something);
  boost::unique_future<int> fi=task.get_future();
  boost::thread thread(boost::move(task));
  int answer = fi.get(); 
}
catch(const some_exception&)
{ cout<< "caught some_exception" << endl;}
catch(const std::exception& err)
{/*....*/}
...
0

精彩评论

暂无评论...
验证码 换一张
取 消