Monitor.Wait 需要同步吗?

does Monitor.Wait Needs synchronization?(Monitor.Wait 需要同步吗?)

本文介绍了Monitor.Wait 需要同步吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了一个通用的生产者-消费者队列,它由 Monitor 以下列方式脉冲:

I have developed a generic producer-consumer queue which pulses by Monitor in the following way:

入队:

    public void EnqueueTask(T task)
    {
        _workerQueue.Enqueue(task);
        Monitor.Pulse(_locker);
    }

出队:

private T Dequeue()
    {
        T dequeueItem;
        if (_workerQueue.Count > 0)
        {
               _workerQueue.TryDequeue(out dequeueItem);
            if(dequeueItem!=null)
                return dequeueItem;
        }
        while (_workerQueue.Count == 0)
            {
                Monitor.Wait(_locker);
        }
         _workerQueue.TryDequeue(out dequeueItem);
        return dequeueItem;
    }

等待部分产生以下 SynchronizationLockException :对象同步方法是从不同步的代码块中调用的"我需要同步吗?为什么 ?使用 ManualResetEvents 还是 .NET 4.0 的 Slim 版本更好?

the wait section produces the following SynchronizationLockException : "object synchronization method was called from an unsynchronized block of code" do i need to synch it? why ? Is it better to use ManualResetEvents or the Slim version of .NET 4.0?

推荐答案

是的,当前线程需要拥有"监视器才能调用 WaitPulse,如文件所述.(因此您还需要锁定 Pulse.)我不知道为什么需要它的详细信息,但在 Java 中是一样的.不过,我通常发现我还是想这样做,以使调用代码干净.

Yes, the current thread needs to "own" the monitor in order to call either Wait or Pulse, as documented. (So you'll need to lock for Pulse as well.) I don't know the details for why it's required, but it's the same in Java. I've usually found I'd want to do that anyway though, to make the calling code clean.

注意 Wait 释放监视器本身,然后等待 Pulse,然后在返回之前重新获取监视器.

Note that Wait releases the monitor itself, then waits for the Pulse, then reacquires the monitor before returning.

至于使用 ManualResetEventAutoResetEvent 代替 - 你可以,但我个人更喜欢使用 Monitor 方法,除非我需要其他一些方法等待句柄的特性(例如以原子方式等待任何/所有多个句柄).

As for using ManualResetEvent or AutoResetEvent instead - you could, but personally I prefer using the Monitor methods unless I need some of the other features of wait handles (such as atomically waiting for any/all of multiple handles).

这篇关于Monitor.Wait 需要同步吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Monitor.Wait 需要同步吗?

基础教程推荐