Given two threads A and B, is there any portable way that A can suspend B, without any custom code in B for that purpose?
Rationale: currently, B is a very complicated algorithm, which should cleanly exit when A (a monitoring thread) tells it to, by checking some shared flag from time to time. The problem is that for debugging purposes I would like to know exactly in which state B is the moment A requests such an exit (e.g. to see where we forgot to check the shared flag), so I would like to pause B (for debugging) from A. In other words, I need to not only synchronize the threads, but synchronize debugging the threads.
It depends on the system. Windows has a function
SuspendThread, but I’ll admit that I don’t know much about it. Under Unix, you should be able to send a signal to the thread usingpthread_kill(which despite its name, doesn’t kill the thread, but just sends a signal), and catch it in the targeted thread, where the signal handler can callsleep.