Are threads waiting on a lock FIFO?

后端 未结 5 1858
被撕碎了的回忆
被撕碎了的回忆 2021-01-05 03:13

Let\'s say I have the following code

static class ...
{
    static object myobj = new object();

    static void mymethod()
    {
        lock(myobj)
                


        
5条回答
  •  执笔经年
    2021-01-05 03:58

    Windows and the CLR attempt their best to guarantee the fairness (the FIFO order) of the waiting. However, there are certain scenarios where the order of the threads waiting on a lock can be changed, mostly revolving around alertable waits and all CLR thread locking puts the thread in alertable state.

    For all practical purposes, you can assume that the order will be FIFO; however, be aware of this issue.

提交回复
热议问题