In .NET, suppose thread A locks an object. Meanwhile, thread B and thread C are blocked and wait for the object to be unlocked by thread A.
Now, thread A unlocked the object. which of the threads (B or C) will be chosen next? how is it determined?
The short answer is that it is non-deterministic – i.e. you never know.
The medium answer is that threads waiting to acquire a lock are put in the “ready queue”, which is FIFO, but you can’t rely on that.
The long answer is that threads in the ready queue can be “borrowed” to run small pieces of work called APCs (
Asynchronous Procedure Calls) When this happens, they lose their place in the queue and when the APC is finished, they are put back in the ready queue – but at the end.So, back to the short answer: you never know.