Marco A.
Marco A.

Reputation: 43662

Can a thread call SuspendThread passing its own thread ID?

Can a Windows thread suspend itself with SuspendThread()?

I can awake it from another one but, can it call SuspendThread(GetCurrentThreadId())?

Upvotes: 4

Views: 2776

Answers (3)

Paul
Paul

Reputation: 1

As a method of creating reusable threads for work tasks without the overhead of create and terminate tasks, suspend and resume thread could be used to quiesce a thread at the end of the task. When work is dispatch to the thread, resume it.

Upvotes: 0

lapk
lapk

Reputation: 3918

Yes, you can use SuspendThread on current thread. Good read on the topic.

Upvotes: 3

rubenvb
rubenvb

Reputation: 76579

It seems this is possible, but with a slight alteration (see the cygwin mailing list discussing this here):

SuspendThread(GetCurrentThread());

I also found MSDN saying a thread should only suspend itself, but it doesn't make it clear for me. I quote (from here, emphasis mine):

This function is primarily designed for use by debuggers. It is not intended to be used for thread synchronization. Calling SuspendThread on a thread that owns a synchronization object, such as a mutex or critical section, can lead to a deadlock if the calling thread tries to obtain a synchronization object owned by a suspended thread. To avoid this situation, a thread within an application that is not a debugger should signal the other thread to suspend itself. The target thread must be designed to watch for this signal and respond appropriately.

Upvotes: 7

Related Questions