One of the biggest challenges of thread management begins when the threads share non-const data
Data race and critical section
In the context of threads using shared data, you often hear the expressions race condition and critical section. But, what's that?
- Data Race
- A data race is a state, in which at least two threads access shared data at the same time, and at least one of the threads is a writer.
- Critical Section
- A critical section is a section of the code, which not more than one thread should access at any point in time.
In case the program has a race condition, the program behavior is undefined. To say it differently, anything can happen.
A nice way to visualize a race condition is to let a few threads write to std::cout. std::cout is the shared object (output stream), that should be protected from simultaneous access by multiple threads.
The boss assigns three work packages (lines 11 - 17) to each of its six workers (lines 32 - 36). When a worker is done with their work package it screams out loudly to the boss (line 16). When the boss has gotten notifications from all workers, it sends them home (line 45).
What a mess!
The same mess the next day. The workers scream out loudly. Totally unsynchronized.
The first solution is a mutex. A mutex ensures, that each thread exclusively accesses the shared variable std::cout.
A side note: std::cout is thread-safe
- The C++11 standard guarantees, that you must not protect the single characters, written to std::cout. Each character will atomically be written. Of course, it is possible, that more output statements like in the example will interleave. But that is only an optical issue. The program is well defined. The remark is valid for all input and output streams.
Mutex stands for mutual exclusion. It ensures, that only one thread can access a critical section.
The key difference to the first example is the lines 19 to 21. By invoking the methods coutMutex.lock() and coutMutex.unlock(), you define the exclusive section. This section can only be accessed by at most a single thread. The access to std::cout is synchronized and the mess becomes harmonious.
Mutexes have a lot of issues, which I will discuss in the next post. (Proofreader Alexey Elymanov)
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, 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, Daniel Hufschläger, Alessandro Pezzato, Evangelos Denaxas, 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, and Wolfgang Fütterer.
Thanks in particular to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, Rusty Fleming, Ralf Abramowitsch, John Nebel, Mipko, and Alicja Kaminska.
My special thanks to Embarcadero
My special thanks to PVS-Studio
I'm happy to give online seminars or face-to-face seminars worldwide. Please call me if you have any questions.
- Clean Code: Best Practices für modernes C++: 11.10.2022 - 13.10.2022 (Termingarantie / Präsenzschulung)
- Design Pattern und Architekturpattern mit C++: 08.11.2022 - 10.11.2022 (Termingarantie / Online-Schulung)
- Embedded Programmierung mit modernem C++ 31.01.2023 - 02.02.2023 (Präsenzschulung)
- C++20: 18.04.2023 - 20.04.2023 (Präsenzschulung)
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++
- Phone: +49 7472 917441
- Mobil:: +49 176 5506 5086
- German Seminar Page: www.ModernesCpp.de
- English Seminar Page: www.ModernesCpp.net
- Mentoring Page: www.ModernesCpp.org