Monitor.Pulse & Wait - Unexpected Behaviour

此生再无相见时 提交于 2019-12-05 10:46:17

Imagine you're trying to write a producer/consumer queue - you Pulse each time you produce an item, and a consumer needs to wait until there's an item to consume. You'd write code like this:

Foo item;
lock(someMonitor)
{
    while (queue.Count == 0)
    {
        Monitor.Wait(someMonitor);
    }
    item = queue.Dequeue();
}
// Use the item

Suppose you didn't have the while loop, and instead wrote:

Foo item;
lock(someMonitor)
{
    // Broken!
    if (queue.Count == 0)
    {
        Monitor.Wait(someMonitor);
    }
    item = queue.Dequeue();
}
// Use the item

Now suppose you have one thread already waiting, and then another thread just before the lock statement... then a producer pulses the monitor (and adds an item to the queue, of course).

At that point, it's entirely feasible that the thread which hasn't even got to the lock yet will be the first to aquire the lock... at which point by the time the "waiting" thread acquires the lock, the queue would be empty again. With just a single if statement, without looping, you'd end up dequeuing when the queue is empty, which would fail.

With the while loop, you'll wait again until the next item is produced, which is what you really want.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!