.NET 2.0中的严重线程.Monitor问题 [英] Serious Threading.Monitor issues in .NET 2.0

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

问题描述




我一直在调查关键字锁不安全的说法

可能有例外情况。这导致我尝试以下代码,

我认为在.NET 2.0框架中发现了一个严重的错误。

请注意,这样运行得更好,但并不完美,在.NET 1.1上。


注意:这些数字是匹配下面

例外的callstack所需的行号。


// Class WorkItem ...

34 public void DoItSafeLock()

35 {

36试试

37 {

38 Monitor.Enter(this);

39 Thread.Sleep(10);

40}

41最后

42 {

43 Monitor.Exit(this); //< - SynchronizationLockException!

44}

45}


这是我跑步时得到的这个代码在一个不同的线程而不是

创建了持有此方法的对象(WorkItem类):


System.Threading.SynchronizationLockException:对象同步<从未经同步的代码块调用
方法。

在WorkItems.WorkItem.DoItSafeLock()中$ / b $ D \WorkItem.cs:第43行

at WorkItems.Worker.ExecuteWorkItem()in
D:\ LockTest \WorkItems \ Worker.cs:第127行


请注意,错误是在第43行,对象没有自己锁定

(这个)。如果通过第38行的Monitor.Enter(this)调用使它成为
,这怎么可能呢?!?


有没有人知道'在这里?如何输入显示器

但是没有获得锁定?


如果有人愿意,我有一个示例应用程序演示错误

看到代码,只需给我发电子邮件,我就拉上解决方案并发送给你。


先谢谢,

Michael


Michael Kennedy

Hi,

I have been looking into the claim that the keyword lock is not safe
when exceptions are possible. That lead me to try the following code,
which I think has uncovered a serious error in the .NET 2.0 framework.
Note that this runs better, but not perfectly, on .NET 1.1.

Note: The numbers are line numbers needed to match the callstack of the
exception below.

// Class WorkItem ...
34 public void DoItSafeLock()
35 {
36 try
37 {
38 Monitor.Enter(this);
39 Thread.Sleep(10);
40 }
41 finally
42 {
43 Monitor.Exit(this); // <-- SynchronizationLockException!
44 }
45 }

Here''s what I get when I run this code on a different thread than the
one that created the object holding this method (WorkItem class):

System.Threading.SynchronizationLockException: Object synchronization
method was called from an unsynchronized block of code.
at WorkItems.WorkItem.DoItSafeLock() in
D:\LockTest\WorkItems\WorkItem.cs:line 43
at WorkItems.Worker.ExecuteWorkItem() in
D:\LockTest\WorkItems\Worker.cs:line 127

Notice that the error is that on line 43, the object does not hold a
lock on itself (this). How is this possible given that it made it
through the Monitor.Enter(this) call on line 38?!?

Does any one know what''s going on here? How can the monitor be entered
and yet no lock is acquired?

I have a sample application demonstrating the error if any one wants to
see the code, just email me and I''ll zip up the solution and send it
your way.

Thanks in advance,
Michael

Michael Kennedy

推荐答案

问题锁定这个,不要这样做。锁定私人物品

代替。然后看看会发生什么

the problem is locking on "this", don''t do it. lock on a private object
instead. then see what happens


嗨KJ,


这不是问题所在。不这样做是最好的做法,但是它不应该让.NET的Monitor类失败。谢谢你的建议。


Michael


ps - 一旦课程其他对象这是另一个课程。

Hi KJ,

That is not the problem. It is a best practice to not do this, but it
should not fail .NET''s Monitor class. Thanks for the advice though.

Michael

ps - Once classes "other object" is another classes this.


你能发布所有代码(足以复制)吗?

Could you post all the code (enough to repro)?


这篇关于.NET 2.0中的严重线程.Monitor问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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