Zhiyao
Zhiyao

Reputation: 4414

How does C++20's memory model differ from that of C++11?

C++11 introduces a new memory model that lets the abstract machine "running" C++11 code have a notion about multiple threads. It also introduces a set of memory orders by which memory load/store operations abide.

The wikipedia page of C++20 says that it has

a revised memory model.

The reference it gives says that the memory model of C++11 has a number of flaws, which C++20 will revise.

Could someone please give some examples about the problems that come with C++11's memory model, and how that in C++20 would fix it?

Related question: Introduction to C++11's memory model

Upvotes: 30

Views: 2666

Answers (1)

einpoklum
einpoklum

Reputation: 132128

As @PeterM suggests, its' a (subjectively) minor change due to issues discovered ex-post-facto with the formalization of the C++11 memory model.

The old model was defined so that different regimes of memory access could be implemented on common architectures using more or less-costly sets of hardware instructions. Specifically, memory_order_acquire and memory_order_release were supposed to be implementable on ARM and Power CPU architectures using some kind of lightweight fence instructions. Unfortunately, it turns out that they can't (!); and this is also true for NVIDIA GPUs, although those weren't really targeted a decade back.

With this being the case, there were two options:

  1. Implement to fit the standard - possible, but then performance would be pretty bad and that wasn't the idea.
  2. Fix the standard to better accommodate these architectures (while not messing up the model completely)

Option 2 was apparently chosen.

For more details, read:

Upvotes: 14

Related Questions