下面的 _otherThing 字段会被锁保护吗?
class ThreadSafeThing
{
private readonly object _sync = new object();
private SomeOtherThing _otherThing;
public SomeOtherThing OtherThing { get { lock(_sync) return _otherThing; } }
public void UpdateOtherThing(SomeOtherThing otherThing)
{
lock(_sync) _otherThing = otherThing;
}
}
最佳答案
是的。
这与锁无关。 C# 程序是用语句表示的。使用 {} 将多个语句分组为一个 block 。 block 可以在允许单个语句的上下文中使用。请参阅 C# 语言规范第 1.5 节。
关于c# - 我可以在 C# 中的一行上锁定(某物)吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1276401/