With the relaxed semantic, we have no synchronisations and ordering constraints on atomic operations.
With the relaxed semantic, there is only the atomicity of the operations on atomics left.
std::cout << y.load(std::memory_order_relaxed) << " ";
std::cout << x.load(std::memory_order_relaxed) << std::endl;
Now, the questions are very easy to answer. Does the program have well-defined behaviour? Which values for x and y are possible? On the one hand, all operations on x and y are atomic. So the program is well defined. On the other hand, there are no restrictions on the interleavings of the threads. In the end, thread 2 can see the operations on thread 1 in a different order. So this is the first time in our process of ongoing optimizations, that thread 2 can display x == 0 and y == 1. All combinations of x and y are possible.
I'm curious, how the graph of CppMem will look like for x == 0 and y == 1?
atomic_int x= 0;
atomic_int y= 0;
That was the CppMem program. Now to the graph.
Execution for (y=0,x=2000)
The graph shows crystal clear the unintuitive behaviour.
x reads the value 2000 from the writing thread, but y reads the value 0 from the main thread. What happens, although the reading of y is sequenced before the reading of x. Sequenced before exact means, that the operation e:Rrix sb is sequenced-before the operation f:Rrix.
This was the last post in my mini-series about ongoing optimization. So, what's next? There are a lot of issues with the singleton pattern. I'm totally aware of that. But the singleton pattern is an ideal use case for a variable, which has to be initialized in a thread-safe way. From that point on, you can use it without synchronization.
So in the next post, I discuss different ways to initialize a singleton in a multithreading environment. You get the performance numbers and can reason about your uses cases for the thread-safe initialization of a variable.
Thanks a lot to my Patreon Supporters: Matt Braun, Roman Postanciuc, Tobias Zindl, Marko, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Animus24, Jozo Leko, John Breland, espkk, Louis St-Amour, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Robert Blanch, Truels Wissneth, Kris Kafka, Mario Luoni, Neil Wang, Friedrich Huber, lennonli, Pramod Tikare Muralidhara, Peter Ware, Tobi Heideman, Daniel Hufschläger, Red Trip, Alexander Schwarz, Tornike Porchxidze, Alessandro Pezzato, Evangelos Denaxas, Bob Perry, Satish Vangipuram, Andi Ireland, Richard Ohnemus, Satish Vangipuram, and Michael Dunsky.
Thanks in particular to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, Rusty Fleming, and Said Mert Turkal.
My special thanks to Embarcadero
I'm happy to give online seminars or face-to-face seminars worldwide. Please call me if you have any questions.
Standard Seminars (English/German)
Here is a compilation of my standard seminars. These seminars are only meant to give you a first orientation.
- Phone: +49 152 31965939
- German Seminar Page: www.ModernesCpp.de
- English Seminar Page: www.ModernesCpp.net