检查相同变量场景的2个不同线程 [英] 2 different threads checking on same variable scenario

查看:63
本文介绍了检查相同变量场景的2个不同线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试着把这个直接放在我脑海里,这样我才能实现。


我前段时间在c#写了一个小实用工具,用了少量的

个帖子。那个场景是一个主线程手动产生一大堆

工作线程,然后在继续之前等待所有这些线程完成。我相信我使用了一系列ManualResetEvents和WaitAll()方法来做这个
。所有工作线程都使用共享对象(一个常见的地方来收集和输出数据)。我记得不得不使用''lock''(基本上是

监视器类)来同步对这个对象的访问。一切都很棒。那个

就在前一段时间,现在我有了一个新的场景(我觉得更简单)但是我不能在脑子里找到解决方案。我正在使用这个过去的例子

因为它是我对mutlithreaded场景的唯一参考点。


现在,我正在写一个小实用程序(控制台应用程序)使用asynch

编程模式。我只是希望主线程在调用

asynch方法后显示某种

进度指示器(向屏幕写点,无论如何),然后在asnych操作完成后继续。所以

目前代码的结构是为了启动异步操作并且

主线程等待使用WaitOne()直到异步操作

表示manualResentEvent。现在我想实现进度

指标,但我不知道如何。我假设我需要摆脱

manualResetEvent和waitOne()设置并转移到检查两个线程可以访问的某个变量




while(sharedVar){做进度指示器的东西}

根据我从我描述的旧项目中学到的东西,我立即

以为只使用锁。但后来我认为锁定永远不会从进度指示器循环中释放出来,以便其他线程获得锁定并修改sharedVar以便循环可以结束?


关于我应该如何处理这个场景的任何输入将会非常感谢

赞赏。

I am trying to get this straight in my head so I can implement.

I wrote a small utility some time ago in c# that utilized a handful of
threads. That scenario was one main thread manually spawning a handfull of
worker threads, then waiting for all of them to complete before moving on. I
believe I used array of ManualResetEvents and the WaitAll() method to do
this. All the worker threads used a shared object (a common place to
collect and output data). I recall having to use ''lock'' (essentially the
monitor class) to synchronize access to this object. All works great. That
was a while ago and now I have a new scenario (much simpler, I think) but I
can''t get a solution straight in my head. I''m using this past example
because it is my only point of reference for mutlithreaded scenarios.

now, I''m writting a small utility (console app) that is using the asynch
programming pattern. I simply want the main thread to show some kind of
progress indicator (writing dots to screen, whatever) after calling an
asynch method and then move on after asnych operation completes. So
currently the code is structured so that the asynch operation is started and
the main thread just waits using WaitOne() until the asynch operation
signals the manualResentEvent. Now I want to implement the progress
indicator but I''m not sure how. I''m assuming I need to get rid of the
manualResetEvent and waitOne() setup and move to checking some variable that
both threads have access to?

while (sharedVar) {do progress indicator thing}

Based on what I learned from the older project I described, I immediately
thought to just use lock. But then I thought that the lock would never be
released from the progress indicator loop in order for the other thread to
gain the lock and modify the sharedVar so the loop could end?

any input on how I should be handling this scenario would be greatly
appreciated.

推荐答案

" David" < no **** @ nospam.comwrote in message

news:%2 **************** @ TK2MSFTNGP02.phx.gbl ...
"David" <no****@nospam.comwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...

while(sharedVar){do progress indicator thing}
while (sharedVar) {do progress indicator thing}



如果sharedVar是布尔值,你不需要任何锁定。将

a值分配给bolean变量并测试布尔变量的值是

线程安全操作。

If the sharedVar is a boolean, you don''t need any kind of lock. Assigning
a value to a bolean variable and testing the value of a boolean variable are
thread-safe operations.


5月9日下午3:31,Alberto Poblacion < earthling-

quitaestoparacontes ... @ poblacion.orgwrote:
On May 9, 3:31 pm, "Alberto Poblacion" <earthling-
quitaestoparacontes...@poblacion.orgwrote:

while(sharedVar){做进度指示器的事情}
while (sharedVar) {do progress indicator thing}



如果sharedVar是一个布尔值,你不需要任何锁。将一个值分配给一个bolean变量并测试一个布尔变量的值是

线程安全操作。


If the sharedVar is a boolean, you don''t need any kind of lock. Assigning
a value to a bolean variable and testing the value of a boolean variable are
thread-safe operations.



不成立,除非将sharedVar声明为volatile。如果它不是b / b
volatile,那就没有什么可以阻止读取线程缓存

值并且永远不会从主内存重新读取它。


我要么让它变得不稳定,要么把它作为一个拿出

锁来阅读和写作的财产。


Jon

Not true, unless sharedVar is declared to be volatile. If it''s not
volatile, there''s nothing to stop the reading thread from caching the
value and never rereading it from main memory.

I''d either make it volatile, or make it a property which took out a
lock for reading and writing.

Jon


感谢Alberto,

这让我想起了我在上一个项目中获得的一些信息......这是因为读/写布尔值是一个''原子''操作?

想知道我是否记得正确。我认为我学习原子操作的同一个地方也谈到了使用''volatile''关键字

声明要使用的变量?我是否需要以任何特殊方式声明我的布尔

shardVar?


再次感谢,我很感激。我前段时间调查过这个东西但是

自从没用过它......我只是把一个非常小的实用工具组合在一起

我需要它做得快,所以我真的希望不必经历我上次做过的所有研究。你节省了我很多时间。谢谢。


" Alberto Poblacion" < ea ****************************** @ poblacion.orgwro te
消息新闻中的
: ************** @ TK2MSFTNGP06.phx.gbl ...
thanks Alberto,
thats reminds me of some of the info I got during the last project... is
this because reading/writing boolean value is an ''atomic'' operation?
Wondering if I''m remembering correctly. I think the same place that I
learned of atomic operations also talked about using ''volatile'' keyword when
declaring the variables to be used like this? Do I need to declare my bool
shardVar in any special way?

Thanks again, I appreciate it. I looked into this stuff some time ago but
havn''t used it since... I''m just putting together a really small utility
that I need to do fast so I was really hoping to not have to go through all
the research I did last time. You are saving me a lot of time. Thank you.

"Alberto Poblacion" <ea******************************@poblacion.orgwro te
in message news:Of**************@TK2MSFTNGP06.phx.gbl...

" David" < no **** @ nospam.comwrote in message

news:%2 **************** @ TK2MSFTNGP02.phx.gbl ...
"David" <no****@nospam.comwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...

> while(sharedVar){do progress indicator thing}
>while (sharedVar) {do progress indicator thing}



如果sharedVar是布尔值,你不需要任何锁定。

为bolean变量赋值并测试boolean

变量的值是线程安全的操作。 br />


If the sharedVar is a boolean, you don''t need any kind of lock.
Assigning a value to a bolean variable and testing the value of a boolean
variable are thread-safe operations.



这篇关于检查相同变量场景的2个不同线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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