Say, if I have three threads that all access the same mutually exclusive part via a mutex.
Let me give you this example.
The first thread probes the mutex and gets its ownership first:
//THREAD 1
//TIME: 2013-03-13 01:00:00.000Z
WaitForSingleObject(hMutex, INFINITE);
//Performs the operation that lasts 50 ms
ReleaseMutex(hMutex);
Then 10 ms later the thread 2 also requests it:
//THREAD 2
//TIME: 2013-03-13 01:00:00.010Z
WaitForSingleObject(hMutex, INFINITE);
//Do work
ReleaseMutex(hMutex);
and 20 ms later thread 3 requests it as well:
//THREAD 3
//TIME: 2013-03-13 01:00:00.030Z
WaitForSingleObject(hMutex, INFINITE);
//Do work
ReleaseMutex(hMutex);
In this situation, can I be sure that thread 2 will always get the mutex ownership before thread 3 (since it was "first in line" so to speak), or is it completely arbitrary who gets the ownership between threads 2 and 3?
And if it is arbitrary with mutexes, how to make sure that the first waiting thread gets the ownership first?