Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 263
Marco A. Avatar asked Dec 26 '22 23:12

Marco A.


1 Answers

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.

like image 196
rubenvb Avatar answered Apr 29 '23 22:04

rubenvb