Boost logo

Boost Users :

From: af001 (fanzigui_at_[hidden])
Date: 2007-06-28 21:07:28


thanks

I get it, btw, I am using version 1.34.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_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>
>

-- 
View this message in context: http://www.nabble.com/Why-can%27t-I-catch-this-exception-in-boost%3A%3Athread--tf3992963.html#a11353524
Sent from the Boost - Users mailing list archive at Nabble.com.

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