【发布时间】:2010-09-08 17:37:40
【问题描述】:
在 C# 中访问 bool 字段是原子的吗?特别是,我需要加锁吗:
class Foo
{
private bool _bar;
//... in some function on any thread (or many threads)
_bar = true;
//... same for a read
if (_bar) { ... }
}
【问题讨论】:
-
是的,但(可能)也是。是的,访问/设置 bool 字段是原子的,但 if 操作不是(请参阅下面 Dror Helper 的答案),因此您可能仍然需要锁。
标签: c# .net concurrency locking boolean