Broes De Cat
Broes De Cat

Reputation: 554

Sleep thread from another one

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.

Upvotes: 11

Views: 515

Answers (1)

James Kanze
James Kanze

Reputation: 153919

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 using pthread_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 call sleep.

Upvotes: 6

Related Questions