Multiple cachelines inside your multi-core machine make a distributed system. Cacheline coherence is 100% a distributed system problem. C++ tries to p

Learning C++ Memory Model from a Distributed System's Perspective

submited by
Style Pass
2021-10-24 00:00:05

Multiple cachelines inside your multi-core machine make a distributed system. Cacheline coherence is 100% a distributed system problem. C++ tries to provide a high level abstraction for writing software that runs on a distributed system via std::memory_order. It makes a lot of sense to expose ordering as an API, because if there's a single key word in distributed system, it would be ordering.

This abstraction makes so much sense that rust just copied it word for word, even though it's pretty convoluted. Just to give you an idea about how convoluted it is, there is many types of happens-before in C++20 standard – sequenced-before, dependency-ordered-before, inter-thread happens-before, happens-before, simply happens-before, strongly happens-before, and coherence-ordered-before. Happens-before relationship defines a partial order. So given these aforementioned happens-before relationships, there are a few types of orderings – release-consume ordering, release-acquire ordering, sequentially-consistent ordering, etc..

Notice that these settings mostly are not describing the requirements on the atomic operation you are performing. They are mostly for non-atomic memory accesses around the atomic operation (except memory_order_seq_cst). All accesses (reads and writes) on a single std::atomic are consistent with the modification order; notice not the evaluation order.

Leave a Comment