互锁性能提升

编程入门 行业动态 更新时间:2024-10-21 11:29:42
本文介绍了互锁性能提升的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

更多推荐

互锁性能提升

本文发布于:2023-10-29 03:45:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1538630.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:性能   互锁

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!