Guarded Suspension applies a unique strategy to deal with mutation. It signals when it is done with its modification.
The guarded suspension basic variant combines a lock and a precondition that must be satisfied. If the precondition is not fulfilled, that checking thread puts itself to sleep. The checking thread uses a lock to avoid a race condition that may result in a data race or a deadlock.
Various variants of the Guarded Suspension exist:
- The waiting thread can passively be notified about the state change or actively ask for the state change. In short, I call this push versus pull principle.
- The waiting can be done with or without a time boundary.
- The notification can be sent to one or all waiting threads.
I present in this post only the rough idea. For further information, I refer to posts I have already written.
Push versus Pull Principle
Let me start with the push principle.
You often synchronize threads with a condition variable or a future/promise pair. The condition variable or the promise sends the notification to the waiting thread. A promise has no
notify_all member function. Typically, a valueless
set_value call is used to signal a notification. The following program snippets show the thread sending the notification and the waiting thread.
- Condition Variable
- Future/Promise Pair
Instead of passively waiting for the state change, you can actively ask for it. This pull principle is not natively supported in C++ but can be, for example, implemented with atomics.
Waiting with and without Time Boundary
A condition variable and a future have three member functions for waiting:
wait_for variant requires a time duration, and the
wait_until variant a time point.
The consumer thread waits for the time duration
steady_clock::now() + dur. The future asks for the value; if the promise is not done, it displays its id: t
Notifying one or all waiting Threads
notify_one awakes one of the waiting threads,
notify_all awakes all the waiting threads. With
notify_one, you have no guarantee which one will be awakened. The other threads do stay in the wait state. This could not happen with a
std::future, because there is a one-to-one association between the future and the promise. If you want to simulate a one-to-many association, use a
std::shared_future instead of a
std::future because a
std::shared_future can be copied.
The following program shows a simple workflow with one-to-one and one-to-many associations between promises and futures.
The key idea of the program is that the
boss (main-thread) has three workers:
herb (line 1),
scott (line 3), and
bjarne (line 3). A thread represents each worker. In line (4), the boss waits until all workers complete their work package preparation. This means each worker sends, after an arbitrary time, the notification to the boss that he is done. The worker-to-the-boss notification is a one-to-one relation because it uses a
std::future (line 5). In contrast, the instruction to start the work is a one-to-many notification (line 6) from the boss to its workers. For this one-to-many notification, a
std::shared_future is necessary.
In my next post, I will focus on concurrent architecture and write about the Active Object.
A Short Break
I will take a short two weeks holiday break. My next post will be published on Monday, the 19th of June.
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, 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, Rob North, and Bhavith C Achar.
Thanks, in particular, to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, Rusty Fleming, John Nebel, Mipko, Alicja Kaminska, Slavko Radman, and David Poole.
|My special thanks to Embarcadero|
|My special thanks to PVS-Studio|
|My special thanks to Tipi.build|
|My special thanks to Take Up Code|
I’m happy to give online seminars or face-to-face seminars worldwide. Please call me if you have any questions.
- Embedded Programmierung mit modernem C++ 12.12.2023 – 14.12.2023 (Präsenzschulung, Termingarantie)
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++
- Clean Code with Modern C++
- Phone: +49 7472 917441
- Mobil:: +49 176 5506 5086
- Mail: schulung@ModernesCpp.de
- German Seminar Page: www.ModernesCpp.de
- Mentoring Page: www.ModernesCpp.org
Modernes C++ Mentoring,