Transitivity of the Acquire-Release Semantic

Contents[Show]

A release operation synchronises with an acquire operation on the same atomic variable and establishes, in addition, ordering constraints. These are the components to synchronise 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 synchronised, which act independently of each other.

Transitivity

In the following example, the 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 totally deterministic. mySharedWork will have the values 1,2 and 3.

trans

 Why is the program totally deterministic? There are two important 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 important parts of the picture are the arrows.

  • The blow arrows are the sequenced-before relations. That means, that 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 synchronisation 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 short and concise post. But that is my plan in order 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, 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, Michael Dunsky, Dimitrov Tsvetomir, Leo Goodstadt, Eduardo Velasquez, John Wiederhirn, and Yacob Cohen-Arazi.

 

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 CBUIDER STUDIO FINAL ICONS 1024 Small

 

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.

New

Contact Me

Modernes C++,

RainerGrimmSmall

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

My Newest E-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

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

Subscribe to the newsletter (+ pdf bundle)

Blog archive

Source Code

Visitors

Today 3843

Yesterday 8323

Week 36723

Month 110133

All 7172423

Currently are 180 guests and no members online

Kubik-Rubik Joomla! Extensions

Latest comments