原子/互锁变量与具有或不具有争用的锁相比,速度有多快? [英] How fast is an atomic/interlocked variable compared to a lock, with or without contention?
问题描述
与无争议的原子变量(例如C ++的std::atomic<T>
)操作相比,速度快/慢得多.
And how much faster/slower it is as compared to an uncontested atomic variable (such as std::atomic<T>
of C++) operation.
此外,有争议的原子变量相对于无争议的锁要慢多少?
Also, how much slower are contested atomic variables relative to the uncontested lock?
我正在研究的体系结构是x86-64.
The architecture I'm working on is x86-64.
推荐答案
在GitHub上有一个项目,旨在对此进行测量在不同的平台上.不幸的是,在完成我的硕士论文之后,我从未真正有时间跟进此事,但至少有了基本的代码.
There’s a project on GitHub with the purpose of measuring this on different platforms. Unfortunately, after my master thesis I never really had the time to follow up on this but at least the rudimentary code is there.
与__sync_fetch_and_add
内在函数相比,它测量pthread和OpenMP锁定.
It measures pthreads and OpenMP locks, compared to the __sync_fetch_and_add
intrinsic.
据我所记得,我们期望锁和原子操作之间存在很大差异(〜一个数量级),但实际差异却很小.
From what I remember, we were expecting a pretty big difference between locks and atomic operations (~ an order of magnitude) but the real difference turned out to be very small.
但是,现在在我的系统上进行测量会得出反映我最初猜测的结果,即(无论使用pthread还是OpenMP)原子操作快大约五倍,而单个锁定增量操作大约需要35ns(这包括获取锁,执行增量,然后释放锁.
However, measuring now on my system yields results which reflect my original guess, namely that (regardless of whether pthreads or OpenMP is used) atomic operations are about five times faster, and a single locked increment operation takes about 35ns (this includes acquiring the lock, performing the increment, and releasing the lock).
这篇关于原子/互锁变量与具有或不具有争用的锁相比,速度有多快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!