Exception propagation to parent thread.
I have come up with some code that allows non-clone-able objects to be
propagated, without slicing, in to the "parent thread" upon join() or
materialisation of a future etc. I was spurred on by a discussion in
this thread on comp.programming.threads:
The static types of the exceptions to be caught must be registered with
the thread (object) before running its "body". Here's some example
// This function will form the "body" of the thread
// The following two statements can be re-ordered without
// changing the outcome. Exceptions that have these exact
// *static* types will be propagated. Any other exceptions
// caught inside the thread are turned in to an exception of
// a special type (uncaught_thread_exception in my code).
std::cout << "Thread is running..." << std::endl;
double d = t.join();
catch(const std::runtime_error &ex)
std::cerr << ex.what() << '\n';
Thread is running...
The full code is here: http://www.nunswithguns.net/guff/propagate.cpp
I'm pretty sure the code could be modified to factor out a
catch/copy/storage policy for each registered type to allow
clone()-able types to be "caught polymorphically" and propagated, too.
Perhaps this has been done before, but I haven't come across it (the
non-slicing-static-type stuff, that is -- I've seen this for
clone()-able hierarchies). It's certainly not perfect, but I thought
I'd throw it out there for the sake of discussion. I know I might well
end up using something like this in the near future.
What do others think?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]