不明白Monitor.Pulse的需要() [英] Don't understand the need for Monitor.Pulse()

查看:160
本文介绍了不明白Monitor.Pulse的需要()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 MSDN Monitor.Wait()




发布的对象,并阻止当前线程上的锁,直到
重新获取锁




不过,凡事我看了一下wait()和脉冲()似乎表明,简单地释放锁在另一个线程是不够的。我需要首先调用脉冲()来唤醒等待线程。



我的问题是为什么呢?线程等待在Monitor.Enter锁()只得到它时,它的释放。没有必要为唤醒他们。这似乎击败wait()方法的有效性。



如:

 静态对象_lock =新的对象(); 

静态无效的主要()
{
新的Thread(计数)。开始();
休眠(10);

锁(_lock)
{
Console.WriteLine(主线程抓起锁);
Monitor.Pulse(_lock)//这是为什么需要时我们大概要释放锁?
}
}

静态无效的计数()
{
锁(_lock)
{
诠释计数= 0;

,而(真)
{
的WriteLine(计数+计数++);

//给其他线程有机会每10迭代
如果(计数%10 == 0)
Monitor.Wait(_lock);
}
}
}

如果我用退出()和Enter键(),而不是等待()我可以做的:

 静态对象_lock =新的对象(); 

静态无效的主要()
{
新的Thread(计数)。开始();
休眠(10);

锁(_lock)Console.WriteLine(主线程抓起锁);
}

静态无效的计数()
{
锁(_lock)
{
诠释计数= 0;

,而(真)
{
的WriteLine(计数+计数++);

//给其他线程有机会每10迭代
如果(计数%10 == 0)
{
Monitor.Exit(_lock);
Monitor.Enter(_lock);
}
}
}
}


解决方案

您使用输入 / 退出来获得一个锁独占访问。



您使用等待 / 脉冲,允许合作社通知:我想等待发生什么,所以我请输入锁和呼叫等待;通知代码将进入锁定并调用脉冲



这两个方案是相关的,但它们不是试图完成同样的事情。



想想你如何实现生产者/消费者队列,其中消费者可以说我叫醒,当你有一个项目我消费没有这样的事情。


According to MSDN, Monitor.Wait():

Releases the lock on an object and blocks the current thread until it reacquires the lock.

However, everything I have read about Wait() and Pulse() seems to indicate that simply releasing the lock on another thread is not enough. I need to call Pulse() first to wake up the waiting thread.

My question is why? Threads waiting for the lock on a Monitor.Enter() just get it when it's released. There is no need to "wake them up". It seems to defeat the usefulness of Wait().

eg.

static object _lock = new Object();

static void Main()
{
    new Thread(Count).Start();
    Sleep(10);

    lock (_lock)
    {
         Console.WriteLine("Main thread grabbed lock");
         Monitor.Pulse(_lock) //Why is this required when we're about to release the lock anyway?
    }
}

static void Count()
{
    lock (_lock)
    { 
        int count = 0;

        while(true)
        {
            Writeline("Count: " + count++);

            //give other threads a chance every 10th iteration
            if (count % 10 == 0)
                 Monitor.Wait(_lock);
        }
    }
}

If I use Exit() and Enter() instead of Wait() I can do:

static object _lock = new Object();

static void Main()
{
    new Thread(Count).Start();
    Sleep(10);

    lock (_lock) Console.WriteLine("Main thread grabbed lock");
}

static void Count()
{
    lock (_lock)
    { 
        int count = 0;

        while(true)
        {
            Writeline("Count: " + count++);

            //give other threads a chance every 10th iteration
            if (count % 10 == 0)
            {
                 Monitor.Exit(_lock);
                 Monitor.Enter(_lock);
            }
        }
    }
}

解决方案

You use Enter / Exit to acquire exclusive access to a lock.

You use Wait / Pulse to allow co-operative notification: I want to wait for something to occur, so I enter the lock and call Wait; the notifying code will enter the lock and call Pulse.

The two schemes are related, but they're not trying to accomplish the same thing.

Consider how you'd implement a producer/consumer queue where the consumer can say "Wake me up when you've got an item for me to consume" without something like this.

这篇关于不明白Monitor.Pulse的需要()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆