同步问题 [英] synclock questions

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

问题描述

我对在几个不同的场景中是否需要synclock有一些疑问:


1)我有一个Queue类,它在两个之间共享线程。线程1

将对象推送到队列中,线程2将对象从队列中弹出

一次一个(没有枚举器 - 只是先进入,先出)。当按下对象或

弹出时,队列类需要同步吗?


2)我有一个boolean变量,可以由线程2读取或写入,

但是对线程1是只读的。这个变量是否必须是

当它被任何一个读取时它是同步的线程或由线程2更改?


谢谢,

基思

解决方案

2003-12-10,Keith Langer< ta ****** @ aol.com>写道:

我对在几个不同场景中是否需要synclock有一些疑问:

1)我有一个Queue类,它在两个线程之间共享。线程1
将对象推入队列,线程2一次一个地从队列中弹出对象(没有枚举器 - 只是先入,先出)。当推送对象或弹出对象时,是否需要同步Queue类?


除非队列是同步的,否则 - 你会想要锁定访问

的推送和弹出。

2)我有一个布尔变量,可以由线程2读取或写入,但是对于线程1是只读的。当该线程被线程读取时,该变量是否需要同步?由第2个线程更改?




再次,是的。如果一个值只读,这不是问题,但是当它被一个线程写入并被另一个线程读取时,总是有一个

的上下文机会开关可能会在

无效状态下留下共享变量。


-

Tom Shelton

MVP [Visual Basic]


Tom,

2)我有一个布尔变量,可以是由线程2读取或写入,
但是对线程1是只读的。当该线程由任一线程读取或由线程2更改时,该变量是否需要同步?
同样,是。如果一个值只读,这不是问题,但是当它被一个线程写入并被另一个线程读取时,总是有一个上下文切换的可能性,这可能会留下一个共享变量
无效状态。



我会说,也许或不是。布尔值是一个原子值(32位或更少)

一个线程是保证的。由CLR能够读取值或写入没有另一个线程腐败的值。这个价值。


至少这是我阅读类库设计指南的方式。

http://msdn.microsoft.com/library/de。 ..Guidelines.asp


我没有关于原子价值的参考资料。 CLR中的线程本身也很方便。


如果线程2需要读取&修改值&

线程1应该在读取期间等待修改周期。


希望这有帮助

Jay


Tom Shelton < to*@mtogden.com>在消息中写道

news:uu ************** @ tk2msftngp13.phx.gbl ... 2003-12-10,Keith Langer< ta * *****@aol.com>写道:

我对在几个不同场景中是否需要synclock有一些疑问:

1)我有一个Queue类,它在两个线程之间共享。线程1
将对象推入队列,线程2一次一个地从队列中弹出对象(没有枚举器 - 只是先入,先出)。当推送对象或弹出对象时,是否需要同步Queue类?



除非队列同步,否则是 - 你会想要在push和pop上锁定访问权限。

2)我有一个布尔变量,可以由线程2读取或写入,但是被读取 - 只有线程1.当这个变量被任一线程读取或被线程2改变时,它是否需要同步?



再次,是的。如果一个值只读,这不是问题,但是当它被一个线程写入并被另一个线程读取时,总是有一个上下文切换的可能性,这可能会留下一个共享变量无效状态。

- 汤姆谢尔顿
MVP [Visual Basic]



< blockquote>在文章< #d ************** @ TK2MSFTNGP10.phx.gbl>中,Jay B. Harlow [MVP - Outlook]写道:

Tom ,

> 2)我有一个布尔变量,可由线程2读取或写入,
>但是对于线程1是只读的。这个变量是否必须是
>当它被任一线程读取或被线程2更改时会同步?



再次,是的。如果一个值只读,这不是问题,但是当它被一个线程写入并被另一个线程读取时,总是有一个上下文切换的可能性,这可能会留下一个共享变量
无效状态。


我想说,也许或不是。布尔值是一个原子值(32位或更小)
一个线程是保证的。通过CLR能够读取值或写入值而无需另一个线程腐败这个价值。

至少这是我阅读类库设计指南的方式。

http://msdn.microsoft.com/library/de...Guidelines。 asp

我没有关于原子价值的参考资料。 CLR中的线程本身很方便。

如果线程2需要读取&修改值&
线程1应该在读取期间等待修改周期。

希望这有帮助
周杰伦




周杰伦,


你是正确的。在这种情况下,没有腐败的可能,

但是在较大的

方案中仍然存在同步问题的可能性。但是再次阅读他的情况,在这种情况下可能不需要

,因为一个线程正在阅读而另一个正在阅读。但是,如果他正在进行任何类型的阅读和阅读,那么这将是b $ b。修改,如你所说,他会想要同步这些代码部分。


线程是一个强大的工具 - 但仍然非常复杂。有一天,我希望

真正理解它:)


-

Tom Shelton

MVP [Visual Basic]


I have some questions about whether synclock is necessary in a few
different scenarios:

1) I have a Queue class which is shared between two threads. Thread 1
pushes objects onto the queue and Thread 2 pops objects off the queue
one at a time (with no enumerators - just first in, first out). Does
the Queue class need to be synclocked when objects are pushed on or
popped off?

2) I have a boolean variable which can be read or written by Thread 2,
but is read-only to Thread 1. Does this variable need to be
synclocked when it is read by either thread or changed by thread 2?

thanks,
Keith

解决方案

On 2003-12-10, Keith Langer <ta******@aol.com> wrote:

I have some questions about whether synclock is necessary in a few
different scenarios:

1) I have a Queue class which is shared between two threads. Thread 1
pushes objects onto the queue and Thread 2 pops objects off the queue
one at a time (with no enumerators - just first in, first out). Does
the Queue class need to be synclocked when objects are pushed on or
popped off?
Unless the queue is syncronized, then yes - you''ll want to lock access
on the push and pop.
2) I have a boolean variable which can be read or written by Thread 2,
but is read-only to Thread 1. Does this variable need to be
synclocked when it is read by either thread or changed by thread 2?



Again, yes. If a value is readonly this isn''t a problem, but when it is
being written by one thread and read by another, there is always a
chance of a context switch which may leave a shared variable in an
invalid state.

--
Tom Shelton
MVP [Visual Basic]


Tom,

2) I have a boolean variable which can be read or written by Thread 2,
but is read-only to Thread 1. Does this variable need to be
synclocked when it is read by either thread or changed by thread 2?
Again, yes. If a value is readonly this isn''t a problem, but when it is
being written by one thread and read by another, there is always a
chance of a context switch which may leave a shared variable in an
invalid state.


I would say, maybe or no. As a boolean is an atomic value (32bits or less)
one thread is "guaranteed" by the CLR to be able to read the value or write
the value without another thread "corrupting" that value.

At least that is the way I read the Class Library Design Guidelines.

http://msdn.microsoft.com/library/de...Guidelines.asp

I don''t have a reference on atomic values & threads in the CLR itself handy.

I would be more concerned if thread 2 need to read & modify the value &
thread 1 should wait during the read & modify cycle.

Hope this helps
Jay

"Tom Shelton" <to*@mtogden.com> wrote in message
news:uu**************@tk2msftngp13.phx.gbl... On 2003-12-10, Keith Langer <ta******@aol.com> wrote:

I have some questions about whether synclock is necessary in a few
different scenarios:

1) I have a Queue class which is shared between two threads. Thread 1
pushes objects onto the queue and Thread 2 pops objects off the queue
one at a time (with no enumerators - just first in, first out). Does
the Queue class need to be synclocked when objects are pushed on or
popped off?



Unless the queue is syncronized, then yes - you''ll want to lock access
on the push and pop.

2) I have a boolean variable which can be read or written by Thread 2,
but is read-only to Thread 1. Does this variable need to be
synclocked when it is read by either thread or changed by thread 2?



Again, yes. If a value is readonly this isn''t a problem, but when it is
being written by one thread and read by another, there is always a
chance of a context switch which may leave a shared variable in an
invalid state.

--
Tom Shelton
MVP [Visual Basic]



In article <#d**************@TK2MSFTNGP10.phx.gbl>, Jay B. Harlow [MVP - Outlook] wrote:

Tom,

> 2) I have a boolean variable which can be read or written by Thread 2,
> but is read-only to Thread 1. Does this variable need to be
> synclocked when it is read by either thread or changed by thread 2?



Again, yes. If a value is readonly this isn''t a problem, but when it is
being written by one thread and read by another, there is always a
chance of a context switch which may leave a shared variable in an
invalid state.


I would say, maybe or no. As a boolean is an atomic value (32bits or less)
one thread is "guaranteed" by the CLR to be able to read the value or write
the value without another thread "corrupting" that value.

At least that is the way I read the Class Library Design Guidelines.

http://msdn.microsoft.com/library/de...Guidelines.asp

I don''t have a reference on atomic values & threads in the CLR itself handy.

I would be more concerned if thread 2 need to read & modify the value &
thread 1 should wait during the read & modify cycle.

Hope this helps
Jay



Jay,

you are correct. In this case there is no chance of corruption,
but there is still a chance of syncronization problems in the larger
scheme of things. But reading his situation again, it may not be needed
in this case because one thread is reading and the other writing. But,
if he is doing any kind of read & modify, as you said, he will want to
sync those sections of code.

Threading is a powerful tool - but still very complex. One day, I hope
to truely understand it :)

--
Tom Shelton
MVP [Visual Basic]


这篇关于同步问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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