线程 - Monitor.Wait / Pulse [英] threading - Monitor.Wait/Pulse

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

问题描述

我有一些奇怪的线程同步问题,需要我

更好地理解Monitor.Wait / Pulse的复杂性。


我有3个主题。线程1在SyncLock块中执行Monitor.Wait

保护资源。线程2和3也有一个SyncLock块保护相同的资源,并且在他们的块中执行一些代码之后它们都会执行
a Monitor.Pulse来锁定资源线程1.


当线程1的资源被锁定时,线程2和3都被阻止在

,它们的SyncLock开始等待资源是释放。

问题是:当线程1执行Monitor.Wait什么决定了哪个线程2或3允许进入其SyncLock的顺序?
?如果线程2得到资源然后用Monitor.Pulse释放它,那么会线程3然后

获取资源还是线程1首先得到它?


谢谢

Perry

I am having some strange thread synchronization problems that require me to
better understand the intricacies of Monitor.Wait/Pulse.

I have 3 threads. Thread 1 does a Monitor.Wait in a SyncLock block
protecting a resource. Thread 2 and 3 also have a SyncLock block protecting
the same resource and after executing some code in their blocks they both do
a Monitor.Pulse to hand of the locked resource back to thread 1.

While thread 1 has the resource locked both thread 2 and 3 are blocked at
the beginning of their SyncLock waiting for the resource to be released. The
question is: When thread 1 does a Monitor.Wait what determines the order of
which thread 2 or 3 is allowed to enter their SyncLock? And if thread 2 gets
the resource and then releases it with a Monitor.Pulse, will thread 3 then
get the resource or will thread 1 get it first?

Thanks
Perry

推荐答案

由于.Net托管线程由本机支持操作系统线程

对象,然后操作系统根据它提供的时隙决定哪个线程以什么

顺序执行。影响

执行顺序的唯一方法是使用线程优先级,这通常被认为是坏事。当Thread1在监视器对象上调用Wait时,它所做的所有

都放弃了剩余的时间段,告诉操作系统

它可以切换到一个不同的线程。每当调用Wait()时,Monitor对象就会将项添加到

队列中,因此您可以非常确定等待的

线程将按照它们最初执行的顺序发出信号,但是你

真的无法控制特定线程何时达到Wait()

语句。


" Perecli Manole写道:
Since .Net Managed threads are backed by native operating system thread
objects, then the operating system decides which thread executes in what
order based on the time slots it has provided. The only way to influence the
order of execution is to muck with Thread Priorities, which is generally
considered a Bad Thing. When Thread1 calls Wait on the monitor object, all
it is doing is giving up the remainder of it''s time slot, and telling the OS
it can switch to a different thread. The Monitor object adds items to a
queue whenever they call Wait(), so you can be pretty sure that the waiting
threads will be signaled in the order they originally executed, but you
really have no control over when a specific thread will reach that Wait()
statement.

"Perecli Manole" wrote:
我有一些奇怪的线程同步问题,需要我更好地理解Monitor.Wait / Pulse的复杂性。

我有3个主题。线程1在SyncLock块中执行Monitor.Wait保护资源。线程2和3还有一个SyncLock块保护相同的资源,并且在他们的块中执行一些代码之后,它们都执行Monitor.Pulse将锁定的资源交还给线程1。
当线程1的资源被锁定时,线程2和3都在其SyncLock开始时被阻塞,等待资源被释放。
问题是:当线程1执行Monitor.Wait什么决定了哪个线程2或3被允许进入其SyncLock的顺序?如果线程2获得资源,然后使用Monitor.Pulse释放它,那么会线程3然后获取资源还是线程1首先得到它?

谢谢< Perry
I am having some strange thread synchronization problems that require me to
better understand the intricacies of Monitor.Wait/Pulse.

I have 3 threads. Thread 1 does a Monitor.Wait in a SyncLock block
protecting a resource. Thread 2 and 3 also have a SyncLock block protecting
the same resource and after executing some code in their blocks they both do
a Monitor.Pulse to hand of the locked resource back to thread 1.

While thread 1 has the resource locked both thread 2 and 3 are blocked at
the beginning of their SyncLock waiting for the resource to be released. The
question is: When thread 1 does a Monitor.Wait what determines the order of
which thread 2 or 3 is allowed to enter their SyncLock? And if thread 2 gets
the resource and then releases it with a Monitor.Pulse, will thread 3 then
get the resource or will thread 1 get it first?

Thanks
Perry



我遇到的问题是线程2和3都在执行他们的

在将资源返回到线程1之前的SyncLock块。我在

假设下,只要线程2或3执行其SyncLock块

(都包含.Pulse)一旦线程2或3完成但不是两者,控制将被返回到线程1

。由于线程2和

3都在线程1释放的同时在SyncLock语句的开头处为

资源释放,它们似乎都在

放弃资源。怎样才能有线程2或3(以
为准)先执行,但在资源返回之前不要同时执行两次

线程1.

谢谢

Perry

" Casey Kramer" <钙********* @ discussions.microsoft.com>在留言中写道

新闻:F3 ********************************** @ microsof t.com ...
The problem I am having is that both threads 2 and 3 are executing their
SyncLock block before returning the resource to thread 1. I was under the
assumption that as soon as either thread 2 or 3 executes its SyncLock block
(both containing a .Pulse statement) control would be given back to thread 1
as soon as either thread 2 or 3 is finished but not both. Since thread 2 and
3 where both waiting at the beginning of the SyncLock statement for the
resource to be released by thread 1, they seem to both execute before
relinquishing the resource. How can have either thread 2 or 3 (whichever
gets there first) execute but not both before the resource is returned to
thread 1.

Thanks
Perry
"Casey Kramer" <Ca*********@discussions.microsoft.com> wrote in message
news:F3**********************************@microsof t.com...
由于.Net托管线程由本机操作系统线程
对象支持,然后操作系统决定哪个线程以什么顺序执行基于它提供的时间段。影响执行顺序的唯一方法就是使用线程优先级,这通常被认为是一件坏事。当Thread1在监视器对象上调用Wait时,
它正在做的就是放弃剩下的时间段,然后告诉
OS它可以切换到一个不同的线程。 Monitor对象在调用Wait()时会将项添加到
队列中,因此您可以非常确定
等待线程将按照它们最初执行的顺序发出信号,但是您<实际上无法控制特定线程何时达到Wait()
语句。

Perecli Manole写道:
Since .Net Managed threads are backed by native operating system thread
objects, then the operating system decides which thread executes in what
order based on the time slots it has provided. The only way to influence
the
order of execution is to muck with Thread Priorities, which is generally
considered a Bad Thing. When Thread1 calls Wait on the monitor object,
all
it is doing is giving up the remainder of it''s time slot, and telling the
OS
it can switch to a different thread. The Monitor object adds items to a
queue whenever they call Wait(), so you can be pretty sure that the
waiting
threads will be signaled in the order they originally executed, but you
really have no control over when a specific thread will reach that Wait()
statement.

"Perecli Manole" wrote:
我有一些奇怪的线程同步问题需要我
更好地理解Monitor.Wait / Pulse的复杂性。
<我有3个帖子。线程1在SyncLock块中执行Monitor.Wait保护资源。线程2和3也有一个SyncLock块保护相同的资源,并且在他们的块中执行一些代码后,他们都会做一个Monitor.Pulse来处理锁定的资源回到主题1.

当线程1的资源被锁定时,线程2和3都被阻止在他们的SyncLock开始处等待资源被释放。

问题是:当线程1执行Monitor.Wait是什么决定了哪个线程2或3允许进入其SyncLock的命令
?如果线程2获得了资源,然后用Monitor.Pulse释放它,那么会线程3
然后获取资源还是线程1首先得到它?

谢谢
Perry
I am having some strange thread synchronization problems that require me
to
better understand the intricacies of Monitor.Wait/Pulse.

I have 3 threads. Thread 1 does a Monitor.Wait in a SyncLock block
protecting a resource. Thread 2 and 3 also have a SyncLock block
protecting
the same resource and after executing some code in their blocks they both
do
a Monitor.Pulse to hand of the locked resource back to thread 1.

While thread 1 has the resource locked both thread 2 and 3 are blocked at
the beginning of their SyncLock waiting for the resource to be released.
The
question is: When thread 1 does a Monitor.Wait what determines the order
of
which thread 2 or 3 is allowed to enter their SyncLock? And if thread 2
gets
the resource and then releases it with a Monitor.Pulse, will thread 3
then
get the resource or will thread 1 get it first?

Thanks
Perry



你能否回答我提出的更重要的最后一个问题在我的上一篇

帖子中?


"如果线程2获得资源,然后用

Monitor发布它。脉冲,将线程3然后获取资源或将线程1得到它

第一个?"


谢谢

佩里

" Casey Kramer" <钙********* @ discussions.microsoft.com>在留言中写道

新闻:F3 ********************************** @ microsof t.com ...
Could you also answer the more important last question I posed in my last
post?

"And if thread 2 gets the resource and then releases it with a
Monitor.Pulse, will thread 3 then get the resource or will thread 1 get it
first?"

Thanks
Perry
"Casey Kramer" <Ca*********@discussions.microsoft.com> wrote in message
news:F3**********************************@microsof t.com...
由于.Net托管线程由本机操作系统线程
对象支持,然后操作系统决定哪个线程以什么顺序执行基于它提供的时间段。影响执行顺序的唯一方法就是使用线程优先级,这通常被认为是一件坏事。当Thread1在监视器对象上调用Wait时,
它正在做的就是放弃剩下的时间段,然后告诉
OS它可以切换到一个不同的线程。 Monitor对象在调用Wait()时会将项添加到
队列中,因此您可以非常确定
等待线程将按照它们最初执行的顺序发出信号,但是您<实际上无法控制特定线程何时达到Wait()
语句。

Perecli Manole写道:
Since .Net Managed threads are backed by native operating system thread
objects, then the operating system decides which thread executes in what
order based on the time slots it has provided. The only way to influence
the
order of execution is to muck with Thread Priorities, which is generally
considered a Bad Thing. When Thread1 calls Wait on the monitor object,
all
it is doing is giving up the remainder of it''s time slot, and telling the
OS
it can switch to a different thread. The Monitor object adds items to a
queue whenever they call Wait(), so you can be pretty sure that the
waiting
threads will be signaled in the order they originally executed, but you
really have no control over when a specific thread will reach that Wait()
statement.

"Perecli Manole" wrote:
我有一些奇怪的线程同步问题需要我
更好地理解Monitor.Wait / Pulse的复杂性。
<我有3个帖子。线程1在SyncLock块中执行Monitor.Wait保护资源。线程2和3也有一个SyncLock块保护相同的资源,并且在他们的块中执行一些代码后,他们都会做一个Monitor.Pulse来处理锁定的资源回到主题1.

当线程1的资源被锁定时,线程2和3都被阻止在他们的SyncLock开始处等待资源被释放。

问题是:当线程1执行Monitor.Wait是什么决定了哪个线程2或3允许进入其SyncLock的命令
?如果线程2获得了资源,然后用Monitor.Pulse释放它,那么会线程3
然后获取资源还是线程1首先得到它?

谢谢
Perry
I am having some strange thread synchronization problems that require me
to
better understand the intricacies of Monitor.Wait/Pulse.

I have 3 threads. Thread 1 does a Monitor.Wait in a SyncLock block
protecting a resource. Thread 2 and 3 also have a SyncLock block
protecting
the same resource and after executing some code in their blocks they both
do
a Monitor.Pulse to hand of the locked resource back to thread 1.

While thread 1 has the resource locked both thread 2 and 3 are blocked at
the beginning of their SyncLock waiting for the resource to be released.
The
question is: When thread 1 does a Monitor.Wait what determines the order
of
which thread 2 or 3 is allowed to enter their SyncLock? And if thread 2
gets
the resource and then releases it with a Monitor.Pulse, will thread 3
then
get the resource or will thread 1 get it first?

Thanks
Perry



这篇关于线程 - Monitor.Wait / Pulse的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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