下面的例子,在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/