« Return to Thread: Why can't I catch this exception in boost::thread?

Re: Why can't I catch this exception in boost::thread?

by af001 :: Rate this Message:

Reply to Author | View in Thread

thanks

I get it, btw, I am using version 1.34.0 with msvc8.0

Andrew Holden wrote:
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 mailing list
Boost-users@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users

 « Return to Thread: Why can't I catch this exception in boost::thread?