'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())?



Solution 1:[1]

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.

Solution 2:[2]

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

Solution 3:[3]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 rubenvb
Solution 2 lapk
Solution 3 Paul