Boost logo

Boost Users :

Subject: [Boost-users] [Thread] Interrupting a thread causes a segmentation fault
From: W Frane (molluskadvertising_at_[hidden])
Date: 2011-02-16 13:08:42


Hello,

I'm new to the Boost libraries and I've been attempting to implement basic multithreading using the Boost Thread library. I'm currently experiencing a problem: when I interrupt a thread my program crashes with a segmentation fault. I've done quite a bit of research on the internet regarding this problem and, although similar problems have been reported before, none of the solutions have worked for me (a number of people have indicated that interrupts work when linking with the static library, just not with the dynamic library, but I was using the static library to begin with).

Technical details are:
Boost version: 1.45.0
Compiler: MinGW 4.4.1
IDE: Code Blocks 10.05

The build log output from Code Blocks is:

mingw32-g++.exe -Wall -fexceptions -g -I"C:\Program Files\boost\boost_1_45_0" -c "C:\Documents and Settings\WFrane\My Documents\C++ code\boost thread interrupt test\boost_thread_interrupt_test\main.cpp" -o obj\Debug\main.o

mingw32-g++.exe -L"C:\Program Files\boost\boost_1_45_0\stage\lib" -o bin\Debug\boost_thread_interrupt_test.exe obj\Debug\main.o "C:\Program Files\boost\boost_1_45_0\stage\lib\libboost_thread-mgw44-mt-d-1_45.a"

(I've also tried a release build with the appropriate version of the Boost.Thread library and the resulting program experiences the same problem.)

I've included a short program below that exhibits this problem. Any suggestions as to what might be causing this and how it could be resolved would be appreciated.

Regards,

W. Frane

EXAMPLE CODE:

#define BOOST_THREAD_USE_LIB // This line was necessary to link with the static libraries.
#include
#include

using namespace std;

boost::mutex mutexCout;

void testingThread(){
    try{
        while(1){
            boost::this_thread::interruption_point();
        }
    }
    catch(boost::thread_interrupted const& e){
        // Thread was interrupted.
        mutexCout.lock();
        cout << "THREAD: Ending." << endl;
        mutexCout.unlock();

    }
    catch(...){
        mutexCout.lock();
        cout << "THREAD: Unknown exception." << endl;
        mutexCout.unlock();
    }
}

int main(int argc, char* argv[])
{

    boost::thread testThread(testingThread);

    mutexCout.lock();
    cout << "MAIN: Created thread; sleeping now." << endl;
    mutexCout.unlock();

    boost::this_thread::sleep(boost::posix_time::seconds(4));

    mutexCout.lock();
    cout << "MAIN: About to interrupt thread." << endl;
    mutexCout.unlock();

    testThread.interrupt(); // Segmentation fault occurs around here during program execution.

    mutexCout.lock();
    cout << "MAIN: Sent interrupt to thread." << endl;
    mutexCout.unlock();

    testThread.join();

    cout << "MAIN: Thread has ended." << endl;

    return 0;
}
                                               


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net