Boost logo

Boost :

From: Eric Woodruff (Eric.Woodruff_at_[hidden])
Date: 2002-08-04 20:56:12


I've developed a way to support user exceptions in threads that I would like
to contribute to boost. In my opinion, it can replace/extend (in the literal
sense) the existing thread class (I also think the existing manor to provide
platform neutrality with ifndefs is unacceptable, but that is for a new
message thread.). At the moment I have created a boost::advanced_thread with
usage as simple as:

boost::advanced_thread<ReturnType, OrderedTupleOfExceptions (default =
none)>

Upon access to any (most -- debatable) of the public methods of the
advanced_thread, a user exception may be thrown. Exceptions not listed to
the class will cause std::bad_exception to be thrown (I believe that is
appropriate). Of course, advanced_thread<void> does not provide
getReturnValue () {or return_value () to follow STL naming}.

For instance:

#include <boost/advanced_thread.hpp>
#include <boost/bind.hpp>

int testFunction () {
    throw 16;
    return 11;
}

int main () {

    typedef boost::advanced_thread<int, thread_exceptions<int, std::string>
> Thread;

    try {
        Thread testThead (boost::bind (testFunction));
        int const returnValue = testThread.getReturnValue (); // implicit
join
    }
    catch (int number) {
        std::cout << number << " was thrown!" << std::endl;
    }
    catch (std::bad_exception& exception) {
        std::cout << "An invalid exception was thrown by the thread user
function. " << std::endl;
    }
}


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