Questions tagged «interlocked»

9
易失性,连锁性与锁定
假设一个类具有一个public int counter可由多个线程访问的字段。这int仅递增或递减。 要增加此字段,应使用哪种方法,为什么? lock(this.locker) this.counter++;, Interlocked.Increment(ref this.counter);, 将的访问修饰符更改counter为public volatile。 现在我已经发现volatile,我已经删除了许多lock语句和对的使用Interlocked。但是,有理由不这样做吗?
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.