本文介绍了互锁性能提升的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Interlocked.Increment(ref x)在各种平台上的整数和多头交易是否比 x ++ 快或慢?
Is Interlocked.Increment(ref x) faster or slower than x++ for ints and longs on various platforms?
推荐答案它速度较慢,因为它会强制执行原子操作,并充当内存屏障,从而消除了处理器重新执行操作的能力。指令周围的顺序内存访问。
It is slower since it forces the action to occur atomically and it acts as a memory barrier, eliminating the processor's ability to re-order memory accesses around the instruction.
您应该使用Interlocked.Increment,当您希望动作在可以在线程之间共享的状态下是原子的时,这不是故意的完全替代x ++。
You should be using Interlocked.Increment when you want the action to be atomic on state that can be shared between threads - it's not intended to be a full replacement for x++.
更多推荐
互锁性能提升
发布评论