使用锁而不是原子内在函数的开销 [英] Overhead of using locks instead of atomic intrinsics

查看:95
本文介绍了使用锁而不是原子内在函数的开销的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道已发布的锁定开销基准,而不是仅仅依靠肯定的原子操作/本能(在多处理器系统上)吗?

Does anyone know of published benchmarks of the overhead of locking instead of relying on certainly atomic operations/intrinsics (on a multiprocessor system) only?

我对一般性结论特别感兴趣,例如例如不管平台如何,锁定至少比内在函数慢X倍". (这就是为什么我不能仅仅对自己进行基准测试.)

I’m particularly interested in general conclusions, e.g. something like "regardless of the platform, locking is at least a factor X slower than intrinsics." (That’s why I can’t just benchmark myself.)

我对直接比较感兴趣,例如

#pragma omp atomic
++x;

代替

#pragma omp critical
++x;

(假设x的每个其他更新也很重要).

(assuming that every other update of x is also critical).

基本上,我需要用它来证明一种复杂的无锁实现,而不是在没有饥饿的情况下直接锁死.传统观点认为,虽然锁定更简单,但非锁定实现具有许多优点.但是我很难找到可靠的数据.

Basically, I need this to justify a complex lock-free implementation instead of a straightforward locking one where starvation isn’t an issue. Conventional wisdom is that while locking is simpler, non-locking implementations have tons of advantages. But I’m hard pressed to find reliable data.

推荐答案

我不知道具体的研究在哪里,但是您不可能在任何地方找到确定的,更好的答案.这在很大程度上取决于您如何使用它们,它们受到多少争用以及您使用原语的目的.如果您只想增加数字,是的,您可能会发现原子图元比锁更快,但是如果您要执行多字比较和交换,或对树结构进行复杂的更新等,则可以将会发现,无锁代码不仅更加复杂且难以调试,而且相对于精心设计的基于锁的实现而言,其性能优势充其量是不确定的,几乎不值得在复杂性上大幅度提高. TANSTAAFL.

I don't know where specific studies are, but you are unlikely to find a definitive locks-are-better answer anywhere. It depends very much on how you use them, how much contention they are subject to, and what you are using the primitives for. If all you want to do is increment numbers, then yes, you'll probably find atomic primitives to be faster than locks, but if you want to perform multi-word compare and swap, or complex updates to tree structures, etc., you'll find that the lock-free code is not only much more complex and difficult to debug, but that the performance advantages over a well-designed lock-based implementation are inconclusive at best, and hardly worth the substantial increase in complexity. TANSTAAFL.

这篇关于使用锁而不是原子内在函数的开销的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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