lock(obj) 中的 c# 函数

标签 c#

下面的例子,在i++执行之前会释放锁吗?换句话说,当在锁内调用函数时,锁会被释放吗?

lock (lockerobject /*which is static*/)
{
   call2anotherFunction();
   i++;
}

最佳答案

直到退出lock block ,锁才会被释放。 lock 不知道也不关心你在 block 中执行了什么代码。

事实上,作为一般规则, block 内发生的事情不为 block 外发生的事情所知:

if (condition)
{
    // The if doesn't know what happens in here
}

using (var reader = XmlReader.Create(url))
{
    // using doesn't care what happens in here
    throw new Exception("Unless...");
} // Dispose will be called on reader here

但是 Dispose 只会因为 block 退出而被调用,而不仅仅是因为其中发生了throw

关于lock(obj) 中的 c# 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3310487/

相关文章:

c# - 如何将空值传递给sql中的存储过程

c# - 在没有 for 或 foreach 的情况下使用 LINQ 进行反射

c# - 如何作为共享库异步返回函数的进度

c# - 应用程序虚拟化是如何实现的?

c# - 为什么 FileShare.ReadWrite 在这种情况下不起作用?

c# - 避免 Resharper 在每个声明中添加一个区域

c# - SQLite 错误没有这样的列使用 Entity Framework

c# - 保存原始深度数据

c# - 如何将属性标记为 json 不可序列化?

c# - BlobContainerClient - 获取根目录中的所有 blob