views:

89

answers:

2

Hello,
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

A: 

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.

hkaiser
wow, thats interesting stuff. Fortunately for me this wasn't the problem in my case. Thank you very much for the idea anyway
zitroneneis
A: 

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

zitroneneis