Thread.Monitor&引用 [英] Thread.Monitor & references

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

问题描述

我需要访问引用对象线程安全。我天生的本能

只是使用Monitor.Enter()和Exit()。问题是引用后面的

对象经常变化,所以我对

Monitor的理解表明这不会保护我的对象。示例:


位图bmp =新位图(" x.jpg");

Monitor.Enter(bmp);

bmp = new Bitmap(" y.jpg");

Monitor.Exit(bmp);


上面做的不做什么可能期待,对吗?我的C ++负责人告诉我

我真正想做的就是保护''指针'',而不是'''''''''''''''''''''''''''''''''''''''''''''''''''''''没有其他指针指向这件事。但是我现在在C#中,现在是b $ b,我的脑子很困惑。


所以目前我使用Mutex来保护这类数据,但它似乎

更加笨重。有没有办法使用光滑的监视器(或锁定)语法和

仍然保护这种情况?


-Brett-

解决方案

为什么不直接声明位图的不变伴星


对象bitmapGuard = new object();


并锁定守卫


问候


Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog


nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/< O0**************@TK2MSFTNGP10.phx.gbl>


我需要访问参考对象线程安全。我天生的本能

只是使用Monitor.Enter()和Exit()。问题是引用后面的

对象经常变化,所以我对

Monitor的理解表明这不会保护我的对象。示例:


位图bmp =新位图(" x.jpg");

Monitor.Enter(bmp);

bmp = new Bitmap(" y.jpg");

Monitor.Exit(bmp);


上面做的不做什么可能期待,对吗?我的C ++负责人告诉我

我真正想做的就是保护''指针'',而不是'''''''''''''''''''''''''''''''''''''''''''''''''''''''没有其他指针指向这件事。但是我现在在C#中,现在是b $ b,我的脑子很困惑。


所以目前我使用Mutex来保护这类数据,但它似乎

更加笨重。有没有办法使用光滑的监视器(或锁定)语法和

仍然保护这种情况?


-Brett-

---

收到的邮件经过无病毒认证。

由AVG反病毒系统检查( http://www.grisoft.com)

版本:6.0.771 /病毒库:518 - 发售日期:28/09/2004


[microsoft.public.dotnet.languages.csharp]


hi

你可以使用锁定块或Mutex本身

问候

ansil


" Brett Robichaud"写道:

我需要访问参考对象线程安全。我天生的直觉就是简单地使用Monitor.Enter()和Exit()。问题是引用后面的
对象频繁更改,所以我对监视器的理解表明这不会保护我的对象。示例:

位图bmp =新位图(" x.jpg");
Monitor.Enter(bmp);
bmp =新位图(" y.jpg" );;
Monitor.Exit(bmp);

以上不是人们可能会想到的,对吗?我的C ++负责人对我说,我真正想要做的就是保护''指针'',而不是''指向''的东西,因为我没有其他指向这个东西的指针。但是我现在在C#中,而且我的脑子很困惑。

所以目前我正在使用Mutex保护这类数据,但似乎更加笨拙。有没有办法使用光滑的监视器(或锁定)语法和
仍然保护这种情况?

-Brett-





" Brett Robichaud" <峰; br ************ @ nospam.yahoo.com> écritdansle message

de news: O0 *** ***********@TK2MSFTNGP10.phx.gbl ...

我需要访问参考对象线程安全。我的天性
本能就是简单地使用Monitor.Enter()和Exit()。问题是引用后面的
对象频繁更改,所以我对监视器的理解表明这不会保护我的对象。示例:

位图bmp =新位图(" x.jpg");
Monitor.Enter(bmp);
bmp =新位图(" y.jpg" );
Monitor.Exit(bmp);


参考分配是原子的。所以,如果你所做的只是重新分配bmp

变量,你不需要用Monitor.Enter / Exit保护它。


如果你需要为了保护代码块而不是单个赋值,你需要锁定一个稳定的对象而不是一个改变所有时间的引用。如果你没有任何这样的对象,你可以随时创建一个:

静态只读对象MyLock = new object();


Bruno。

当人们可能期待时,上述不行,对吗?我的C ++负责人告诉我
我真正想做的就是保护''指针'',而不是''指向''的东西,因为我没有其他指针对这件事。但是我现在在C#
我的头脑很困惑。

所以目前我正在使用Mutex来保护这类数据,但似乎
更笨拙。有没有办法使用光滑的监视器(或锁定)语法

仍然保护这种情况?

-Brett-


I need to make access to a reference object threadsafe. My natural instinct
was to simply use Monitor.Enter() and Exit(). The problem is that the
object behind the reference changes frequently, so my understanding of
Monitor indicates this would not protect my object. Example:

Bitmap bmp = new Bitmap("x.jpg");
Monitor.Enter(bmp);
bmp = new Bitmap("y.jpg");
Monitor.Exit(bmp);

The above doesn''t do when one might expect, correct? My C++ head says to me
that what I really want to do is protect the ''pointer'', not thing ''thing
pointed to'', since I have no other pointers to this thing. But I''m in C# now
and my head is baffled.

So currently I am using a Mutex to protect this kind of data, but it seems
more klunky. Is there any way to use the slick Monitor (or lock) syntax and
still protect this situation?

-Brett-

解决方案

Why not just declare an invariant companion to the Bitmap

object bitmapGuard = new object();

and lock the guard instead

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<O0**************@TK2MSFTNGP10.phx.gbl>

I need to make access to a reference object threadsafe. My natural instinct
was to simply use Monitor.Enter() and Exit(). The problem is that the
object behind the reference changes frequently, so my understanding of
Monitor indicates this would not protect my object. Example:

Bitmap bmp = new Bitmap("x.jpg");
Monitor.Enter(bmp);
bmp = new Bitmap("y.jpg");
Monitor.Exit(bmp);

The above doesn''t do when one might expect, correct? My C++ head says to me
that what I really want to do is protect the ''pointer'', not thing ''thing
pointed to'', since I have no other pointers to this thing. But I''m in C# now
and my head is baffled.

So currently I am using a Mutex to protect this kind of data, but it seems
more klunky. Is there any way to use the slick Monitor (or lock) syntax and
still protect this situation?

-Brett-

---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.771 / Virus Database: 518 - Release Date: 28/09/2004

[microsoft.public.dotnet.languages.csharp]


hi
you can either use lock block or Mutex itself
regards
ansil

"Brett Robichaud" wrote:

I need to make access to a reference object threadsafe. My natural instinct
was to simply use Monitor.Enter() and Exit(). The problem is that the
object behind the reference changes frequently, so my understanding of
Monitor indicates this would not protect my object. Example:

Bitmap bmp = new Bitmap("x.jpg");
Monitor.Enter(bmp);
bmp = new Bitmap("y.jpg");
Monitor.Exit(bmp);

The above doesn''t do when one might expect, correct? My C++ head says to me
that what I really want to do is protect the ''pointer'', not thing ''thing
pointed to'', since I have no other pointers to this thing. But I''m in C# now
and my head is baffled.

So currently I am using a Mutex to protect this kind of data, but it seems
more klunky. Is there any way to use the slick Monitor (or lock) syntax and
still protect this situation?

-Brett-




"Brett Robichaud" <br************@nospam.yahoo.com> a écrit dans le message
de news: O0**************@TK2MSFTNGP10.phx.gbl...

I need to make access to a reference object threadsafe. My natural
instinct
was to simply use Monitor.Enter() and Exit(). The problem is that the
object behind the reference changes frequently, so my understanding of
Monitor indicates this would not protect my object. Example:

Bitmap bmp = new Bitmap("x.jpg");
Monitor.Enter(bmp);
bmp = new Bitmap("y.jpg");
Monitor.Exit(bmp);
Reference assignment is atomic. So, if all you do is reassign the bmp
variable, you don''t need to protect it with Monitor.Enter/Exit

If you need to protect a block of code rather than a single assignment, you
should lock on a stable object rather than on a reference that changes all
the time. If you don''t have any such object, you can always create one:
static readonly object MyLock = new object();

Bruno.

The above doesn''t do when one might expect, correct? My C++ head says to
me
that what I really want to do is protect the ''pointer'', not thing ''thing
pointed to'', since I have no other pointers to this thing. But I''m in C#
now
and my head is baffled.

So currently I am using a Mutex to protect this kind of data, but it seems
more klunky. Is there any way to use the slick Monitor (or lock) syntax
and
still protect this situation?

-Brett-



这篇关于Thread.Monitor&amp;引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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