Search code examples
c++multithreadingboostboost-thread

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


Solution

  • 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