简体   繁体   中英

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

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM