Transitivity of the Acquire-Release Semantic

Contents[Show]

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.

 

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.

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.

trans

 Why is the program deterministic? There are two critical observations:

  1. Thread t2 waits in line 18 until thread t1 has set dataProduced on true (line 14).
  2. Thread t3 waits in line 23 until thread t2 has set dataConsumed on true (line 19).

The rest is easier explained with a picture.

transitivity

 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, 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 Fabio 2021-06-28 10:33
May be a mistake:
"Why is the program totally deterministic?
There are two important observations:
1 Thread t2 waits in line 18 until thread t3 has set dataProduced on true (line 14).
2 Thread t1 waits in line 23 until thread t2 has set dataConsumed on true (line 19).
The rest is easier explained with a picture."

I suppose you inverted t1 with t3
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 3810

Yesterday 4344

Week 40688

Month 20934

All 12099143

Currently are 157 guests and no members online

Kubik-Rubik Joomla! Extensions

Latest comments