Boost logo

Boost :

From: Vygis D. (vygintas.daugmaudis_at_[hidden])
Date: 2003-10-16 17:21:04


I have no idea where to report this, so as I am using Boost.Thread library,
I decided to consult with you at first, whether
this phenomenon is rather a compiler issue (I have no access to any but g++
compiler for now) or problems related with
thread library itself. Perhaps I'm myself wrong at all and my code is
supposed to fail? I read standard carefully and unless
I missed something, I got myself absolutely convinced that C++ exceptions
are thread-safe.

// Reduced runtime failing example
// C++ exceptions are supposed to be thread safe but there's an example
// which fails miserably when compiled with
// g++ (GCC) 3.2.3 (mingw special 20030504-1), OS - Windows XP

#include <iostream>
#include <boost/thread/mutex.hpp>
#include <boost/thread/thread.hpp>
#include <boost/thread/xtime.hpp>

namespace {
    // streams are not required to be thread safe
    boost::mutex io_mutex;
}

void thread_proc()
{
    // if 110 or more errors encountered,
    // consider situation to be irrecoverable
    for( std::size_t fail = 0; fail < 110; ++fail )
    {
        try {
            throw std::runtime_error("just exception");
        }
        catch(const std::runtime_error& e) {
            {
                boost::mutex::scoped_lock lock(io_mutex);
                std::cerr << "(" << clock() << ")\tERROR: "
                    << e.what() << std::endl;
            }

            // uncomment this if you want
            // almost immediate crash, approx. 1s after
            // program is started
            /*// suspend thread for 1 second
            boost::xtime xt;
            boost::xtime_get(&xt, boost::TIME_UTC);
            xt.sec += 1;
            boost::thread::sleep(xt);*/
        }
    }
}

int main()
{
    boost::thread_group threads;
    for( std::size_t i=0; i<50; ++i )
        threads.create_thread(&thread_proc);
    threads.join_all();
}

/*
    interesting output:

(5557) ERROR: St9exception
(5557) ERROR: just exception
(5557) ERROR: just exception
(5557) ERROR: just exception
(5557) ERROR: just exception
(5568) ERROR: St9exception
(5568) ERROR: just exception
(5568) ERROR: just exception
(5568) ERROR: St9exception
(5568) ERROR: just exception
(5568) ERROR: just exception
(5568) ERROR: just exception
(5568) ERROR: just exception
(5568) ERROR: St9exception

    where "St9exception" message is thrown?..
*/


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk