The execution order of pending threads blocked by a mutex

I have a mutex that controls access to a single object from multiple threads. When the stream is completed, the mutex is unlocked to allow order flows for the object. On Windows using the WaitForSingleObject function, is there a flow order? I want the first thread that tries to lock the mutex to now block the mutexes. This will be the FIFO queue so that the signaling of blocked threads is not random. Should I implement my own queuing mechanism to achieve this? And if so, what features are useful?

+4
source share
4 answers

The FIFO signal blocks convoys . In newer versions of the Win32 API, the convoy problem is resolved by mutually locking mutexes and other synchronization primitives. dishonest (i.e. FIFO).

If multiple threads are waiting, a mutex is selected that is waiting for the thread. Do not assume the first, the first (FIFO). External events such as nuclear-mode APCs can change the waiting order.

+5
source

Yes, you will need to implement your own queue mechanism if you want a FIFO queue.

0
source

if you want to unlock in FIFO order, you can use custom lock. There is a FIFO lock in ACE; it is called ACE_Token, and since it is open source, perhaps you can use it as a reference implementation. I think that the overhead of using it will be minimal.

0
source

Windows mode for your own planning is the use of fibers. The main thread will wait for the mutex as soon as it returns, you explicitly call SwitchToFiber from the secure QUEUE (FIFO) thread.

  • To invoke the SwitchToFiber call flow, you must call ConvertThreadToFiber
  • SwitchToFiber function should call SwitchToFiber from QUEUE
0
source

All Articles