Ongoing Optimization: Relaxed Semantic with CppMem

Contents[Show]

With the relaxed semantics, we have no synchronizations and ordering constraints on atomic operations.

 

Relaxed Semantics

With the relaxed semantics, only the atomicity of the operations on atomics is left.

 

 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
// ongoingOptimizationRelaxedSemantic.cpp

#include <atomic>
#include <iostream>
#include <thread>

std::atomic<int> x{0};
std::atomic<int> y{0};

void writing(){  
  x.store(2000,std::memory_order_relaxed);  
  y.store(11,std::memory_order_relaxed);
}

void reading(){  
  std::cout << y.load(std::memory_order_relaxed) << " ";  
  std::cout << x.load(std::memory_order_relaxed) << std::endl;
}

int main(){
  std::thread thread1(writing);
  std::thread thread2(reading);
  thread1.join();
  thread2.join();
};

 

Now, the questions are very easy to answer. Does the program have well-defined behavior? 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 interleaving of the threads. In the end, thread two 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 two can display x == 0 and y == 1. All combinations of x and y are possible.

sukzessiveOptimierungRelaxedSemantikEng

 I'm curious, what the graph of CppMem will look like for x == 0 and y == 1?

 

Rainer D 6 P2 540x540Modernes C++ Mentoring

Be part of my mentoring programs:

 

 

 

 

Do you want to stay informed about my mentoring programs: Subscribe via E-Mail.

CppMem

int main(){
  atomic_int x= 0;
  atomic_int y= 0;
  {{{ { 
      x.store(2000, memory_order_relaxed);
      y.store(11,memory_order_relaxed);
      }
  ||| {
      y.load(memory_order_relaxed);
      x.load(memory_order_relaxed);
      }
  }}}
}

 

That was the CppMem program. Now to the graph.

Execution for (y=0,x=2000)

The graph shows crystal clear unintuitive behavior.

relaxed

x reads the value 2000 from the writing thread, but y reads 0 from the main thread. What happens when 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.

What's next?

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 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 use cases for the thread-safe initialization of a variable.

 

 

 

 

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, Animus24, Jozo Leko, John Breland, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Robert Blanch, Truels Wissneth, Kris Kafka, 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, Matthieu Bolt, Stephen Kelley, Kyle Dean, Tusar Palauri, Dmitry Farberov, Juan Dent, George Liao, Daniel Ceperley, Jon T Hess, Stephen Totten, Wolfgang Fütterer, Matthias Grün, Phillip Diekmann, Ben Atakora, Ann Shatoff, and Rob North.

 

Thanks, in particular, to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, Rusty Fleming, John Nebel, Mipko, Alicja Kaminska, and Slavko Radman.

 

 

My special thanks to Embarcadero CBUIDER STUDIO FINAL ICONS 1024 Small

 

My special thanks to PVS-Studio PVC Logo

 

My special thanks to Tipi.build tipi.build logo

 

My special thanks to Take Up Code TakeUpCode 450 60

 

Seminars

I'm happy to give online seminars or face-to-face seminars worldwide. Please call me if you have any questions.

Bookable (Online)

German

Standard Seminars (English/German)

Here is a compilation of my standard seminars. These seminars are only meant to give you a first orientation.

  • C++ - The Core Language
  • C++ - The Standard Library
  • C++ - Compact
  • C++11 and C++14
  • Concurrency with Modern C++
  • Design Pattern and Architectural Pattern with C++
  • Embedded Programming with Modern C++
  • Generic Programming (Templates) with C++

New

  • Clean Code with Modern C++
  • C++20

Contact Me

Modernes C++,

RainerGrimmDunkelBlauSmall

 

Comments   

0 #1 Crystal x asli 2016-11-16 18:59
I simply could not go away your website prior to
suggesting that I actually enjoyed the usual info a person provide on your guests?
Is gonna be back continuously in order to inspect new posts
Quote
0 #2 人妻無料動画 2016-12-01 00:35
Excellent read, I just passed this onto a colleague who was doing some research on that.
And he just bought me lunch since I found it for him smile So
let me rephrase that: Thank you for lunch!
Quote

Stay Informed about my Mentoring

 

Mentoring

English Books

Course: Modern C++ Concurrency in Practice

Course: C++ Standard Library including C++14 & C++17

Course: Embedded Programming with Modern C++

Course: Generic Programming (Templates)

Course: C++ Fundamentals for Professionals

Course: The All-in-One Guide to C++20

Course: Master Software Design Patterns and Architecture in C++

Subscribe to the newsletter (+ pdf bundle)

All tags

Blog archive

Source Code

Visitors

Today 3340

Yesterday 5555

Week 33548

Month 55222

All 12133431

Currently are 187 guests and no members online

Kubik-Rubik Joomla! Extensions

Latest comments