Hacker News new | ask | show | jobs
by yakubin 1457 days ago
Don't atomic operations trigger cache synchronisation in CPUs? Doesn't that affect performance negatively? That would mean even a non-contended mutex would affect performance negatively. I suspect it depends a lot on the specific workload (and maybe even what addresses data is stored at in memory), so I'd measure the specific case, but that's my a priori gut feeling.
2 comments

If it's non-contended, the mutex's cache line probably stays Exclusive in the local CPU and acquiring is pretty cheap.
If there's no contention there's no more synchronization compared to any other cache lines afaik.
Atamic operations have some overhead.
The overhead of atomics is almost (if not entirely?) exclusively with regards to managing the caches in the CPU. Otherwise they're just normal bytes. Your CPU already has to do some cache management with regular bytes, so an atomic is only worse if there's contention (because that forces a flush).

The worst case for an atomic write is two additional cache line flushes, iirc.