views:

39

answers:

1

I've been "playing around with" boost threads today as a learning exercise, and I've got a working example I built quite a few months ago (before I was interrupted and had to drop multi-threading for a while) that's showing unusual behaviour.

When I initially wrote it I was using MingW gcc 3.4.5, and it worked. Now I'm using 4.4.0 and it doesn't - incidentally, I've tried again using 3.4.5 (I kept that version it a separate folder when I installed 4.4.0) and it's still working.

The code is at the end of the question; in summary what it does is start two Counter objects off in two child threads (these objects simply increment a variable then sleep for a bit and repeat ad infinitum - they count), the main thread waits for the user via a cin.get() and then interrupts both threads, waits for them to join, then outputs the result of both counters.

Complied with 3.4.5 it runs as expected.

Complied with 4.4.0 it runs until the user input, then dies with a message like the below - it seems the the interrupt exceptions are killing the entire process?

terminate called after throwing an instance of ' This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more information. boost::thread_interrupted'

This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more information.

From what I read, I think that any (?) uncaught exception that is allowed to propagate out of a child thread will kill the process? But, I'm catching the interrupts here, aren't I? At least I seem to be when using 3.4.5.

So, firstly, have I understood how interrupting works?
And, any suggestions as to what is happening and how to fix?

Code:

#include <iostream>
#include <boost/thread/thread.hpp>
#include <boost/date_time.hpp>

//fixes a linker error for boost threads in 4.4.0 (not needed for 3.4.5)
//found via Google, so not sure on validity - but does fix the link error.
extern "C" void tss_cleanup_implemented() { }

class CCounter
{
private:
    int& numberRef;
    int  step;
public:
    CCounter(int& number,int setStep) : numberRef(number) ,step(setStep) { }

    void operator()()
    {
        try
        {
            while( true )
            {
                boost::posix_time::milliseconds pauseTime(50);
                numberRef += step;
                boost::this_thread::sleep(pauseTime);
            }
        }
        catch( boost::thread_interrupted const& e )
        {
            return;
        }
    }
};

int main( int argc , char *argv[] )
{
    try
    {
        std::cout << "Starting counters in secondary threads.\n";

        int number0 = 0,
            number1 = 0;
        CCounter counter0(number0,1);
        CCounter counter1(number1,-1);

        boost::thread threadObj0(counter0);
        boost::thread threadObj1(counter1);

        std::cout << "Press enter to stop the counters:\n";
        std::cin.get();

        threadObj0.interrupt();
        threadObj1.interrupt();

        threadObj0.join();
        threadObj1.join();

        std::cout << "Counter stopped. Values:\n"
                  << number0 << '\n'
                  << number1 << '\n';
    }
    catch( boost::thread_interrupted& e )
    {
        std::cout << "\nThread Interrupted Exception caught.\n";
    }
    catch( std::exception& e )
    {
        std::cout << "\nstd::exception thrown.\n";
    }
    catch(...)
    {
        std::cout << "\nUnexpected exception thrown.\n"
    }

    return EXIT_SUCCESS;
}
A: 

Solved.

It turns out adding the complier flag -static-libgcc removes the problem with 4.4.0 (and has no apparent affect with 3.4.5) - or at least in this case the program returns the expected results.

DMA57361