Jon关于线程的文章:等待和脉冲 [英] Jon's article on threading: Waiting and Pulsing

查看:58
本文介绍了Jon关于线程的文章:等待和脉冲的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许现在已经很晚了,但我正在阅读Jon关于

线程的文章,特别是如何使用Monitor.Wait()和Monitor.Pulse (),

并且有一些东西没有下沉。有问题的代码

看起来像这样:


public class ProducerConsumer

{

readonly object listLock = new object();

Queue queue = new Queue();


public void Produce(object o)

{

lock(listLock)

{

queue.Enqueue(o);

if(queue.Count == 1)

{

Monitor.Pulse( listLock);

}

}

}


公共对象Consume()

{

lock(listLock)

{

while(queue.Count == 0)

{

Monitor.Wait(listLock);

}

返回queue.Dequeue();

}

}

}


我不明白的是为什么Consume()方法中的代码是封闭在一个锁(listLock)中的
?这不会造成僵局吗? I.e。:

消费在listLock上获取一个锁,然后调用Monitor.Wait等待一个脉冲的
。然后Produce()尝试生成一些东西,但是等待它的lock(listLock)等等。测试,等待Consume()以

释放它对listLock的锁定,当然,它永远不会这样做,

,因为它正在等待Produce()脉冲它。


我知道Jon运行测试他自己的代码:)所以我知道这是有效的,

但是我看不出它可以工作。


有人可以解释一下吗?

Maybe it''s just late in my day, but I''m reading Jon''s article on
threading, in particular how to use Monitor.Wait() and Monitor.Pulse(),
and there''s something that''s not sinking in. The code in question
looks like this:

public class ProducerConsumer
{
readonly object listLock = new object();
Queue queue = new Queue();

public void Produce(object o)
{
lock (listLock)
{
queue.Enqueue(o);
if (queue.Count==1)
{
Monitor.Pulse(listLock);
}
}
}

public object Consume()
{
lock (listLock)
{
while (queue.Count==0)
{
Monitor.Wait(listLock);
}
return queue.Dequeue();
}
}
}

What I don''t understand is why the code in the Consume() method is
enclosed in a lock (listLock)? Won''t this create a deadlock? I.e.:
Consume acquires a lock on listLock, then calls Monitor.Wait to wait
for a pulse. Produce() then attempts to produce something, but gets
stuck waiting on its "lock (listLock)" test, waiting for Consume() to
release its lock on listLock, which, of course, it will never do,
because it''s waiting for Produce() to pulse it.

I know that Jon runs tests his own code :) so I know that this works,
but I can''t see how it could work.

Could someone please explain?

推荐答案



" Bruce Wood" <峰; br ******* @ canada.com>在消息中写道

news:11 ********************** @ o13g2000cwo.googlegr oups.com ...

"Bruce Wood" <br*******@canada.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
也许现在已经很晚了,但我正在阅读Jon关于线程的文章,特别是如何使用Monitor.Wait()和Monitor.Pulse(),
并且有一些东西没有下沉。有问题的代码
看起来像这样:

公共类ProducerConsumer
{
readonly对象listLock = new object();
队列队列=新队列();

public void生成(对象o)
{
lock(listLock)
{
queue.Enqueue(o);
if(queue.Count == 1)
{
Monitor.Pulse(listLock);
}
}


公共对象消耗()
{
lock(listLock)
{while /(queue.Count = = 0)
{
Monitor.Wait(listLoc k);
}
返回队列。取消();
}
}
}

我不明白的是为什么Consume()方法中的代码被封装在一个锁(listLock)中?这不会造成僵局吗? I.e。:
Consume在listLock上获取一个锁,然后调用Monitor.Wait等待一个脉冲。然后Produce()尝试生成某些东西,但是等待其lock(listLock)等等。测试,等待Consume()在listLock上释放它的锁定,当然,它永远不会这样做,因为它正在等待Produce()来激活它。

我知道Jon运行测试他自己的代码:)所以我知道这个有效,
但我看不出它是如何工作的。

有人可以请解释一下?
Maybe it''s just late in my day, but I''m reading Jon''s article on
threading, in particular how to use Monitor.Wait() and Monitor.Pulse(),
and there''s something that''s not sinking in. The code in question
looks like this:

public class ProducerConsumer
{
readonly object listLock = new object();
Queue queue = new Queue();

public void Produce(object o)
{
lock (listLock)
{
queue.Enqueue(o);
if (queue.Count==1)
{
Monitor.Pulse(listLock);
}
}
}

public object Consume()
{
lock (listLock)
{
while (queue.Count==0)
{
Monitor.Wait(listLock);
}
return queue.Dequeue();
}
}
}

What I don''t understand is why the code in the Consume() method is
enclosed in a lock (listLock)? Won''t this create a deadlock? I.e.:
Consume acquires a lock on listLock, then calls Monitor.Wait to wait
for a pulse. Produce() then attempts to produce something, but gets
stuck waiting on its "lock (listLock)" test, waiting for Consume() to
release its lock on listLock, which, of course, it will never do,
because it''s waiting for Produce() to pulse it.

I know that Jon runs tests his own code :) so I know that this works,
but I can''t see how it could work.

Could someone please explain?




是的。


这就是为什么Jon在挂锁或挂锁上做了这样的努力

listLock在这种情况下..

因为他有一个readonly listLock,它(有点)是静态的并且会锁定(和

队列)尝试调用任何方法的任何线程。


或者我理解它...我错过了什么?


哦,你有垃圾邮件中某处的邮件...


- Michael S



Yes.

This is why Jon have made such an effort in writing on ''padlocks'' or a
listLock in this case..
As he has a readonly listLock, it is (somewhat) static and will lock (and
queue) any thread that tries to call any methods.

Or so I understand it... What am I missing?

Oh, You got a mail somewhere in your spam...

- Michael S


Bruce,


Monitor.Wait会在阻塞之前释放锁。在返回呼叫者之前,它将重新获得锁定的
。在线程等待的时间内没有锁定




虽然我已经学会了,但我有一段更难的时间

理解while循环的必要性。每个人都可以看到吗?


Brian


布鲁斯伍德写道:
Bruce,

Monitor.Wait will release the lock before it blocks. It will reacquire
the lock before returning to the caller. The lock is not held during
the time the thread is waiting.

Though I have since learned, I had a much more difficult time
understanding the need for the while loop. Can everybody see it?

Brian

Bruce Wood wrote:
也许它刚刚进入我的一天,但我正在阅读Jon关于线程的文章,特别是如何使用Monitor.Wait()和Monitor.Pulse(),
并且有一些东西'这个代码看起来像这样:

public class ProducerConsumer
{
readonly object listLock = new object();
队列队列=新队列();

public void产生(对象o)
{
lock(listLock)
{queue /Enqueue( o);
if(queue.Count == 1)
{
Monitor.Pulse(listLock);
}
}
}
公共对象消耗()
{
lock(listLock)
{while /(queue.Count == 0)
{
Monitor.Wa它(listLock);
}
返回队列。取消();
}
}
}

我什么都不知道理解为什么Consume()方法中的代码被封装在一个锁(listLock)中?这不会造成僵局吗? I.e。:
Consume在listLock上获取一个锁,然后调用Monitor.Wait等待一个脉冲。然后Produce()尝试生成某些东西,但是等待其lock(listLock)等等。测试,等待Consume()在listLock上释放它的锁定,当然,它永远不会这样做,因为它正在等待Produce()来激活它。

我知道Jon运行测试他自己的代码:)所以我知道这个有效,
但我看不出它是如何工作的。

有人可以请解释一下?
Maybe it''s just late in my day, but I''m reading Jon''s article on
threading, in particular how to use Monitor.Wait() and Monitor.Pulse(),
and there''s something that''s not sinking in. The code in question
looks like this:

public class ProducerConsumer
{
readonly object listLock = new object();
Queue queue = new Queue();

public void Produce(object o)
{
lock (listLock)
{
queue.Enqueue(o);
if (queue.Count==1)
{
Monitor.Pulse(listLock);
}
}
}

public object Consume()
{
lock (listLock)
{
while (queue.Count==0)
{
Monitor.Wait(listLock);
}
return queue.Dequeue();
}
}
}

What I don''t understand is why the code in the Consume() method is
enclosed in a lock (listLock)? Won''t this create a deadlock? I.e.:
Consume acquires a lock on listLock, then calls Monitor.Wait to wait
for a pulse. Produce() then attempts to produce something, but gets
stuck waiting on its "lock (listLock)" test, waiting for Consume() to
release its lock on listLock, which, of course, it will never do,
because it''s waiting for Produce() to pulse it.

I know that Jon runs tests his own code :) so I know that this works,
but I can''t see how it could work.

Could someone please explain?






嗯...没有。但缺乏理解只能在少数情况下阻碍我。 :)

Umm... no. But lack of understanding holds me up in only a few cases. :)


这篇关于Jon关于线程的文章:等待和脉冲的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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