0

I have a bunch of worker threads which I would need to stop from the main thread. As C++11 does not allow to terminate a thread from outside, I would like to throw an exception to each one of them so that they can close on their own. I found a lot of answers on the opposite (catch exceptions thrown in worker threads inside the main one, as in here: How can I propagate exceptions between threads?), but I need to do exactly the opposite! Any ideas?

Thanks, Michele

Michele
  • 31
  • 3
  • Don't use exceptions for this. Use a boolean flag which is periodically checked by workers. – Sebastian Hoffmann Jul 16 '20 at 15:37
  • I cannot modify the workers: they are functors that I receive and instantiate inside a wrapper, but I have means to change their code. – Michele Jul 16 '20 at 16:21
  • "Throw an exception _to_ a thread" makes no sense. An exception can only ever be caught in the same thread from which it was thrown. There might be some way to cause the worker itself to throw an exception under controlled circumstances (e.g., like Java's `thread.interrupt()` mechanism) but if if so then (a) I don't know what it is, and (b) I probably would avoid it for all the same reasons why I avoid using `thread.interrupt()` in Java. – Solomon Slow Jul 16 '20 at 16:23
  • Indeed I meant "to call": the functors are packaged inside plugins that I load at runtime. – Michele Jul 16 '20 at 16:42
  • Maybe exception is the wrong way to go: if is was a Process I would define a handler and send it a signal using its pid. I have been doing this for a long time and it works well. I was hoping there was something similar for threads – Michele Jul 16 '20 at 16:43
  • @Michele Well you definitely have to go to OS-level then. For Win32 there is `TerminateThread` https://learn.microsoft.com/en-gb/windows/win32/api/processthreadsapi/nf-processthreadsapi-terminatethread?redirectedfrom=MSDN – Sebastian Hoffmann Jul 16 '20 at 16:47
  • However, mind the the documentation, which clearly states: "TerminateThread is a dangerous function that should only be used in the most extreme cases. You should call TerminateThread only if you know exactly what the target thread is doing, and you control all of the code that the target thread could possibly be running at the time of the termination. " – Sebastian Hoffmann Jul 16 '20 at 16:48
  • In Linux I did it my calling "pthread_exit", but I was hoping to find a portable solution to work on Windows too. Looks like I need to make OS-specific implementations and control their inclusion (at compilation time or with pre-processor) – Michele Jul 16 '20 at 16:51
  • @Michele ?? pthread_exit terminates the calling thread only? – Martin James Jul 17 '20 at 05:53
  • 'I have a bunch of worker threads which I would need to stop from the main thread', may I as why? You should try to design away that requirement. Why? It's not guaranteed to be safely possible from user code:( – Martin James Jul 17 '20 at 05:57
  • I mean, there are bodges you can sometimes do, eg. if the threads are working on buffers through a pointer, you could null the pointer so that the thread excepts on it's next access, but such kludges are ummm...'not optimal' :) – Martin James Jul 17 '20 at 06:03
  • Similarly, you can somtimes 'orphan' the threads by setting some atomic flag that they occasionally check, then setting them to the lowest possible priority and just ignoring them until they die off:) – Martin James Jul 17 '20 at 06:06

0 Answers0