With the C++0x feature of exception_ptr one can store an exception in one thread and have another thread access it. However, the other thread has to call rethrow_exception(). In some cases, however, I need to actually interrupt the other thread and have the exception be raised there immediately; polling the exception_ptr for non-null is not a solution.
I found a solution for how to inject an exception in Windows at http://www.codeproject.com/KB/exception/ExcInject.aspx by suspending a thread and modifying its instruction pointer register before resuming it. However, I also need my code to run on Linux. How do I accomplish it there?
If I use something like getcontext(), that gets the current thread’s context (whereas in Windows, GetContext() accepts a thread parameter), so I would have to call it in a signal handler. But I’ve read that getcontext()/setcontext() should not be called in signal handlers…
Alternatively, I could call rethrow_exception() directly in the signal handler, but I’m worried this won’t have the intended effect, where the interrupted thread has the stack unwound in the same way with all destructors of the scope the interruption was in called etc.
I read that article you linked to some time ago, and I was a bit skeptical.
The approach of modifying another thread’s instruction pointer directly feels VERY scary. You have absolutely no idea what operation you’re interrupting and leaving half-completed, and the interrupted operation may not be expecting an exception and thus have no ability to clean up after itself.
On Windows, I’ve used QueueUserAPC successfully to abort other threads once they enter an alertable wait state. I don’t know of any direct equivalent for this in Linux.