is a known unsafe technique; for example, this is the reason why the corresponding API in .NET is marked obsolete.
The right approach is this: you should use Windows API event object:
Your thread to be suspended/resumed should call
on the event instance:
When the event object is non-signalled, the thread will be put in the wait state; it will be switched off an not scheduled to execution, thus wasting zero CPU time, until it is awaken. One of the way to awake it is to put the same event object in a signaled state, which you can do in another thread. This way, a thread can be throttled by other threads.
As a thread can be put to wait state only in few points specially selected by the developer, such code can be written in a safe way, in contrast to
which is called asynchronously and can suspend a thread at a "bad" moment of time. A typical example of the disaster: a thread took a mutex object (or critical section; any of the objects used for mutual exclusion
) and was suspended before releasing it; which can create a random (worse) kind of deadlock
, because some other threads use the same mutex object.