Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

c++11

c++20

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

like image 564
Zhiyao Avatar asked Jun 20 '20 15:06

Zhiyao


1 Answers

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:

  • Lahav, Vafeiadis, Kang, Hur, Dreyer, Repairing Sequential Consistency in C/C++11.
  • Hans Boehm's C++-standard-committee paper P0668R5: Revising the C++ memory model.
like image 99
einpoklum Avatar answered Nov 13 '22 05:11

einpoklum