开发者

Using exceptions within a boost::thread thread

I began to play around with boost::threads, but I'm kind of stuck with this problem:

I don't understand why this program crashes as soon as the exception is thrown, because I try to catch it within the thread. I thought that it would be possible to work with exceptions as long as the handling happens in the same thread as the throwing ?

#include <boost/thread.hpp>
#include <exception>

using namespace std;

void doWork();
void thrower();

int main( int argc, char** argv ){
 boost::thread worker(doWork);
 worker.join();
 return 0;
}

void doWork(){
 try{
    thrower();
 }
 catch( const exception &e ){
  //handle exception
 }
}
void thrower(){
 // program terminates as soon as the exception is thrown
 throw exception();
}

Additional information:开发者_开发百科

*Using MinGW32

*Boost v.1.44

*Linking dynamically against the multithread debug dll Version of the thread lib


I found the problem: it's a bug in the boost library that only occurs when working with a minGW Version newer than 3.17. Boost trac ticket #4258

After applying the suggested workaround, and setting the Preprocessor Definition BOOST_THREAD_USE_LIB I am now able to link against the static library, and I can work with exceptions, as long as they're caught in the same thread that throws them.

Tank you very much for your comments


In applications consisting out of multiple shared libraries you might have to be very careful wrt the visibility of your exceptions. gcc does not make the RTTI information for exceptions visible from the outside of a shared library by default, causing exceptions thrown across shared library boundaries 'disappear'. See here for a detailed description and possible pitfalls.

Certainly I can't be sure you're facing this problem, but from what you describe it's a possibility.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜