假设一个类有一个public int counter由多个线程访问的字段.这int只是递增或递减.
public int counter
int
要增加此字段,应使用哪种方法,为什么?
lock(this.locker) this.counter++;
Interlocked.Increment(ref this.counter);
counter
public volatile
现在,我发现volatile,我已经删除了许多lock语句和使用Interlocked.但是有理由不这样做吗?
volatile
lock
Interlocked
c# multithreading locking volatile interlocked
c# ×1
interlocked ×1
locking ×1
multithreading ×1
volatile ×1