views:

380

answers:

4

I have a mutex that controls access to a single object from multiple threads. When a thread has finished the mutex is unlocked to allow order threads to operate on the object. On Windows using the WaitForSingleObject function is there an order that threads are signaled? I want the first thread that attempts to lock the mutex to now be allowed to lock the mutex. This would be a FIFO queue so that signaling to the blocked threads is not random. Would I have to implement my own queuing mechanism to achieve this? And if so what functions are useful?

+4  A: 

FIFO signaling leads to lock convoys. On newer versions of the Win32 API the convoy issue is addressed by macking mutexes and other synchrnonization primitives explicitly unfair (ie. no FIFO).

If more than one thread is waiting on a mutex, a waiting thread is selected. Do not assume a first-in, first-out (FIFO) order. External events such as kernel-mode APCs can change the wait order.

Remus Rusanu
You're quite right that FIFO is explicitly not guaranteed. However, there is also a guarantee that starvation will nonetheless be avoided.
Steven Sudit
@Steven: Right. Afaik periodically the queue head is moved to the tail, which guarantees no *infinite* starvation. Threads may still encounter *temporary* starvation.
Remus Rusanu
A: 

Yes you would need to implement your own queueing mechanism if you want a FIFO queue.

Zanson
A: 

if you want the unlocking to occur in FIFO order, you can use a custom lock. A FIFO lock exist in ACE; it's called ACE_Token, and since it's open source, perhaps you can use it as a reference implementation. i think the overhead of using it will be minimal.

geva30
A: 

The Windows way to do your own scheduling is by using fibers. The main thread will wait on the mutex, once it returns, you explicitly call SwitchToFiber from a thread safe QUEUE (FIFO).

  1. The thread calling SwitchToFiber will need to call ConvertThreadToFiber
  2. The function in SwitchToFiber should call to SwitchToFiber from the QUEUE
hackworks