锁定语义问题 [英] lock semantics question

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

问题描述

说我们有以下内容:

Say we have the following:

private object _lock = new object();
public void A()
{
   lock(_lock)
   {
       // do something critical
       B();
   }
}

public void B()
{
   // do something else
}

然后我们从Task s或Thread s调用A方法,锁操作块是否也覆盖了B方法调用,即B调用也被序列化了?

(我似乎发生了奇怪的事情,只能通过锁中没有发生的B方法来解释-否则我可能会误解.).

And we call the A method from Tasks or Threads does the lock operation block cover the B method call also i.e. B calls are also serialized?

(I seem to have weird things going on which can only be explained by the B method not happening in the lock - or I could be mistaken).

推荐答案

不.方法B未涵盖.

lock 语句确保当两个调用者调用A时,其中一个执行B并在另一个调用者开始执行之前(在lock 语句中的代码执行).
实际上,可以从应用程序中的任何其他位置调用B(而不是通过调用A),并且不会停止执行它!
No. The method B is not covered.

The lock statement makes sure that when two callers call A, one of the executes B and completes before the other one starts execution (of the code within the lock statement).

B could actually be called from anywhere else in the application (not by calling A) and there would be nothing stopping from executing it!


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

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