Transitivity of the Acquire-Release Semantic
A release operation synchronizes with an acquire operation on the same atomic variable and establishes, in addition, ordering constraints. These are the components to synchronize threads in a performant way in case they act on the same atomic. But how can that work, if two threads share no atomic variable? We want no sequential consistency because that is too heavy. We want the light acquire-release semantic.
The answer to the riddle is easy. Because of the transitivity of the acquire-release semantic, threads can be synchronized, which act independently of each other.
Transitivity
In the following example, thread t2, with its work package deliveryBoy is the glue between the two independent threads t1 and t3.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
// transitivity.cpp #include <atomic> #include <iostream> #include <thread> #include <vector> std::vector<int> mySharedWork; std::atomic<bool> dataProduced(false); std::atomic<bool> dataConsumed(false); void dataProducer(){ mySharedWork={1,0,3}; dataProduced.store(true, std::memory_order_release); } void deliveryBoy(){ while( !dataProduced.load(std::memory_order_acquire) ); dataConsumed.store(true,std::memory_order_release); } void dataConsumer(){ while( !dataConsumed.load(std::memory_order_acquire) ); mySharedWork[1]= 2; } int main(){ std::cout << std::endl; std::thread t1(dataConsumer); std::thread t2(deliveryBoy); std::thread t3(dataProducer); t1.join(); t2.join(); t3.join(); for (auto v: mySharedWork){ std::cout << v << " "; } std::cout << "\n\n"; } |
The output of the program is deterministic. mySharedWork will have the values 1,2, and 3.
Modernes C++ Mentoring
Do you want to stay informed: Subscribe.
Why is the program deterministic? There are two critical observations:
- Thread t2 waits in line 18 until thread t1 has set dataProduced on true (line 14).
- Thread t3 waits in line 23 until thread t2 has set dataConsumed on true (line 19).
The rest is easier explained with a picture.
The critical parts of the picture are the arrows.
- The blow arrows are the sequenced-before relations. All operations in one thread will be executed in source code order.
- The red arrows are the synchronize-with relations. The reason is the acquire-release semantic of the atomic operations on the same atomic. So the synchronization between the threads takes place.
- As well sequenced-before as synchronizes-with establishes a happens-before relation.
The rest is pretty simple. The chronological order of the instructions (happens-before) corresponds to the direction of the arrows from top to bottom. So, we have the guarantee that mySharedWork[1] == 2 will be executed last.
What’s next?
That was a concise post. But that is my plan to keep the parts easy to digest. That will change with the next post because I will write about the legendary std::memory_order_consume memory order.
Thanks a lot to my Patreon Supporters: Matt Braun, Roman Postanciuc, Tobias Zindl, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Jozo Leko, John Breland, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Robert Blanch, Truels Wissneth, Mario Luoni, Friedrich Huber, lennonli, Pramod Tikare Muralidhara, Peter Ware, Daniel Hufschläger, Alessandro Pezzato, Bob Perry, Satish Vangipuram, Andi Ireland, Richard Ohnemus, Michael Dunsky, Leo Goodstadt, John Wiederhirn, Yacob Cohen-Arazi, Florian Tischler, Robin Furness, Michael Young, Holger Detering, Bernd Mühlhaus, Stephen Kelley, Kyle Dean, Tusar Palauri, Juan Dent, George Liao, Daniel Ceperley, Jon T Hess, Stephen Totten, Wolfgang Fütterer, Matthias Grün, Phillip Diekmann, Ben Atakora, Ann Shatoff, Rob North, Bhavith C Achar, Marco Parri Empoli, Philipp Lenk, Charles-Jianye Chen, Keith Jeffery,and Matt Godbolt.
Thanks, in particular, to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, Rusty Fleming, John Nebel, Mipko, Alicja Kaminska, Slavko Radman, and David Poole.
My special thanks to Embarcadero | |
My special thanks to PVS-Studio | |
My special thanks to Tipi.build | |
My special thanks to Take Up Code | |
My special thanks to SHAVEDYAKS |
Modernes C++ GmbH
Modernes C++ Mentoring (English)
Rainer Grimm
Yalovastraße 20
72108 Rottenburg
Mail: schulung@ModernesCpp.de
Mentoring: www.ModernesCpp.org
Modernes C++ Mentoring,
Leave a Reply
Want to join the discussion?Feel free to contribute!