Injecting a C++ exception in another thread
With the C++0x feature of exception_ptr one can store an exceptio开发者_StackOverflow中文版n 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 asked more or less the same question here. After some time I found a good answer. Here is the code, you can find more comments in my answer:
#include <thread>
#include <signal.h>
#include <unistd.h>
#include <iostream>
using namespace std;
//Custom exception which is used to stop the thread
class StopException {};
void sig_fun(int s)
{
if(s == SIGUSR2)throw StopException();
}
void threadFunction()
{
cout<<"Thread started"<<endl;
try {
while(true)
{
//Work forever...
sleep(1);
}
} catch(const StopException &e) {
cout<<"Thread interrupted"<<endl;
}
cout<<"Thread stopped"<<endl;
}
int main(int argc, char *args[])
{
//Install Signal handler function
signal(SIGUSR2, sig_fun);
pthread_t threadObject;
thread t([&threadObject]()
{
//Store pthread_t object to be able to use it later
threadObject = pthread_self();
threadFunction();
});
string temp;
cout<<"Write something when you want the thread to be killed"<<endl;
cin>>temp;
//Send Signal to thread
pthread_kill(threadObject, SIGUSR2);
t.join();
}
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.
精彩评论