Suspending a thread that has a synchronizing object is dangerous

2006/7/7

SuspendThread

 

The SuspendThread function suspends the specified thread.

DWORD SuspendThread(
  HANDLE hThread
);

Remarks

... ...

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.

---- from MSDN

Sample of suspending a thread owns a mutex

This sample creates two threads, each drawing a circle synchronizing by a mutex.

when the thread locked the mutex, the circle is drawn in black; the circle is drawn white when mutex unlocked.

If you suspend thread 1 when it locks the mutex, the thread 2 is blocked too.