In the last post “Dining Philosophers Problem I“, Andre Adrian started his analysis of the classical dining philosophers’ problem. Today, he uses atomics, mutexes, and locks.
By Benjamin D. Esham / Wikimedia Commons, CC BY-SA 3.0, https://commons.wikimedia.org/w/index.php?curid=56559
Let me give you a quick reminder about where Andre’s analysis ended last time.
Still Erroneous Busy Waiting with Resource Hierarchy
The program looks fine but has a tiny chance of misbehavior. The two operations “is a resource available” and “mark resource as in use” in the
lock() function is atomic, but they are still two operations. Between these two operations, the scheduler can place a thread switch. And this thread switches at this most inconvenient time can produce hard-to-find bugs in the program.
Optimized Busy Waiting with Resource Hierarchy
Thankfully all current computers have an atomic operation “test the resource, and if the test is positive, mark resource as in use”. In the programming language C++, the
atomic_flag type makes this special “test and set” operation available. The file
dp_6.cpp is the first correct solution for the dining philosophers problem:
atomic_flag spinlock is needed if several threads want to get the same resource.
Good low CPU load Busy Waiting with Resource Hierarchy
lock()is a waste of CPU resources. A remedy to this problem is putting a function in this while loop’s body. The
sleep_for()function performs waiting in the scheduler. This waiting is much better than waiting in the application. As always, there is a price. The
sleep_for()slows down the program’s progress. The file
dp_7.cppis the second correct solution:
std::this_thread::yield()instead of the
sleep_for()does not reduce CPU load on the author’s computer. The impact of
std::mutex with Resource Hierarchy
dp_8.cppshows the mutex solution. Please note the
std::lock_guard with Resource Hierarchy
lock_guardtemplate, we put only the mutex into the lock. The mutex member function
lockis automatically called in the locks constructor and
unlockits destructor at the end of the scope.
unlockis also called if an exception is thrown.
The convenient version is
std::lock_guard and Synchronized Output with Resource Hierarchy
lock_guardaround every console output. See
mocontrols the console output resource. Every
coutstatement is in its block, and the
lock_guard()template ensures that console output is no longer garbled.
std::lock_guard and Synchronized Output with Resource Hierarchy and a count
dp_11.cpp. This program version counts the number of philosophers threads that are eating simultaneously. Because we have 4 forks, there should be times when 2 philosopher threads eat concurrently. Please note that you need again
#include <atomic>. See
The addition is 1 or 2 at the end of the “eats” logging.
In his next installment of the dining philosophers problem, Andre uses
std::scoped_lock (C++17), and
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,