Wednesday 

Room 3 

15:00 - 16:00 

(UTC+02

Talk (60 min)

The Two Memory Models

Programming languages like C, C++, and Rust all define a memory model, which allows you to reason about the ordering of memory operations in your program. This is crucial for the correctness and speed of your multithreaded programs, whether you're using good old mutexes or lock-free algorithms. But did you know that your CPU also has a memory model?

C++
C
Platform

In the first part of this talk, we review some examples of C/C++, both single- and multithreaded, including a common lock-free algorithm. We examine the compiler-generated Assembly code and gain an intuition for how much reordering the compiler and the CPU are allowed to do. We then discuss what C++ means by a data race and how to avoid them using atomics and memory orderings like sequentially consistent, relaxed, acquire, and release. We also see how C++ reasons about memory ordering in terms of happens-before relationships.

In the second part of the talk, we examine the CPU memory models and what we mean by reordering of memory operations. There are many sources of reordering, such as the compiler, the CPU pipeline, micro-ops, memory systems with store buffers and coalescing, caches, etc. We then see how CPUs reason about memory ordering differently than languages like C++ and Rust and how the CPU memory models enable the language memory models to work. We also see how x86, ARM, and RISC-V have different approaches to this.

At the end of this talk, you will have gained a better understanding of what guarantees your programming language gives you and how the CPU works under the hood to allow your programs to (safely) go vroom! You will also have learned not to make assumptions about the correctness of your code by simply looking at the Assembly code.

PS! In the next slot in the same room, Jana Machutová will go more into practical applications of the memory model in C++ with her talk "Memory Model: Get your shared data under control".

Anders Schau Knatten

Anders started programming in Turbo Pascal in 1995, and has been programming professionally in various languages since 2001. He's currently a principal engineer at Ascenium in Oslo, working on a new CPU design. He's also the author of the book C++ Brain Teasers, http://cppquiz.org, and https://blog.knatten.org