Boost logo

Boost Users :

From: Andrew Holden (aholden_at_[hidden])
Date: 2007-06-28 10:11:38


Af001 wrote:
> Why can't I catch this exception in boost::thread?
>
>
> #include <boost/thread/thread.hpp>
> #include <iostream>
> #include <exception>
>
> void helloworld()
> {
> std::cout << "Hello World!" << std::endl;
> throw(std::string("err"));
> }
>
> int main()
> {
> try
> {
> boost::thread thrd(&helloworld);
> thrd.join();
> }
> catch(...)
> {
> std::cout<<"err";
> }
> }

This is a limitation of exceptions in a multi-threaded environment.
Exceptions must be caught in the same thread from which they are thrown.
In your program, you created a new thread for helloworld(). Therefore,
any exceptions thrown by helloworld will cause a runtime error, unless
helloworld itself catches them. You should be able to work around this
my storing the exception in a global variable or something. Perhaps:

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

volatile bool threaderror = false;
std::string error;

void helloworld()
{
        try
        {
                std::cout << "Hello World!" << std::endl;
                throw(std::string("err"));
        }
        catch (std::string &err)
        {
                threaderror = true;
                error = err;
        }
}

int main()
{
        try
        {
                boost::thread thrd(&helloworld);
                thrd.join();
                if (threaderror)
                {
                        throw error;
                }
        }
        catch(...)
        {
                std::cout<<"err";
        }
}

Notice how, in this version, helloworld() catches the exception itself
and stores it in a global variable. Main() then checks the variable and
throws a copy of the original exception.

Does anyone have any ideas for a cleaner version of this? It looks like
boost.thread allows a functor for the initial function, so maybe you
could convert helloworld() to a functor and declare those global
variables I added as member variables, along with a member function to
rethrow any exceptions caught.

Perhaps someone could create a generic version of this? It could maybe
take a MPL list of exception types that it should marshall across
threads. Is there a way to catch *any* exception in the thread and
store it for a later rethrow?


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