4 ms·
In general, relaxed memory models are useful because you do not need to fetch a cache line from another core (slow!) every time you want to perform a read or ev
by Gladdyu 11y ago
In general, relaxed memory models are useful because you do not need to fetch a cache line from another core (slow!) every time you want to perform a read or even possibly a write (although that could introduce a data race). In case of a lock, where you want to guarantee some state being equal for all cores, you'd need strong memory ordering guarantees.
For other applications, where it is only important that things will eventually propagate through, think of adding elements to a vector (which could be serialized in multiple ways anyway) or performing computation on data that is implicitly shared between cores but not actually read by the other core during the operations, a relaxed memory system is sufficient. It's also faster and more power efficient as high-speed core interconnects are a major drain, compared to, lets say the ALU in a modern CPU.
Lock-free structures are useful, but as your data structure gets more complicated and the updates involve more operations, it becomes a very non-trivial task to write a lock-free version of your operation. It's easier to simply lock and perform an 'atomic' update. Immutable structures often have performance overheads which are not desired in the C++ STL.
- pkolaczk 11y agoMinor nitpick: There is no such thing like fetching a cache line from another core, at least in Intel x64 architecture. All the synchronizing between the cores happen at L3 cache layer and QPI, because caches are inclusive and L3 is common to all cores. When a core writes to L3, it invalidates the same cache line in L1 and L2 caches of other cores. Therefore if your cores write to the same cache line, you have a big problem regardless of the chosen memory order model.