互锁性能提升 [英] Performance of Interlocked.Increment

查看:113
本文介绍了互锁性能提升的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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++.

这篇关于互锁性能提升的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆