std::mem_order_consume
This lesson introduces std::mem_order_consume which is used for concurrency in C++.
We'll cover the following
Introduction
That is for two reasons that std::memory_order_consume
is the most legendary of the six memory models: first, std::memory_order_consume
is extremely hard to understand, and second - which may change in the future - no compiler currently supports it. With C++17 the situation gets even worse. Here is the official wording: “The specification of release-consume ordering is being revised, and the use of memory_order_consume is temporarily discouraged.”
How can it be that a compiler that implements the C++11 standard doesn’t support the memory model std::memory_order_consume
? The answer is that the compiler maps std::memory_order_consume
to std::memory_order_acquire
. This is acceptable because both are load or acquire operations; std::memory_order_consume
requires weaker synchronization and ordering constraints than std::memory_order_acquire
. Therefore, the release-acquire ordering is potentially slower than the release-consume ordering, but - and this is the key point - it’s well-defined.
To get an understanding of the release-consume ordering, it is a good idea to compare it with the release-acquire ordering. I speak in the following subsection explicitly about the release-acquire ordering (not about the acquire-release semantic) to emphasize the strong relationship of std::memory_order_consume
and std::memory_order_acquire
.
Release-acquire Ordering
Let’s use the following program with two threads t1
and t2
as a starting point. t1
plays the role of the producer, t2
the role of the consumer. The atomic variable ptr
helps to synchronize the producer and consumer.
Get hands-on with 1400+ tech skills courses.