According to the MSDN:
The Monitor class consists of static (in C#) or Shared (in Visual Basic) methods that operate on an object that controls access to the critical section. The following information is maintained for each synchronized object:
A reference to the thread that currently holds the lock.
A reference to a ready queue, which contains the threads that are ready to obtain the lock.
A reference to a waiting queue, which contains the threads that are waiting for notification of a change in the state of the locked object.
And in this thread, the 2 queues are causing some subtle issue.
I think the root cause of the issue in above thread is there are 2 queues. If there's only ONE queue, whenever the Monitor.Pulse()
, only one thread from that single queue can be scheduled to run. There's no way for more than one threads to be in ready state at the same time. So the issue should never happen.
So why does a Monitor
keep 2 queues?