I have a program that creates a thread that listens for an event. There is a situation in which this thread will never receive this event, and I must terminate it. I know how to catch the case in which it will not receive this event.
I've created a std::thread
, however I found nothing on how to terminate the thread. I've tried calling
t.detach()
And then letting the destructor so the work, but I cannot clean up resources allocated by the thread this way.
My questions are:
1. Is there a way to send a SIGKILL or equivalent to a std::thread
to kill it? And
2. Is there a way to catch this signal from within the thread to clean up resources?
The question asked (and answered) How do I terminate a thread in C++11? doesn't answer my question as I want to terminate the thread from it's parent, not from within the thread.
Furthermore, my problem is that my worker thread can be either waiting for a blocking system call (i.e., an incoming packet) or a conditional variable, which is why this is tricky.