Dealing with Mutation: Thread-Safe Interface -- Rainer Grimm
I continue my journey with concurrency patterns in today's post. The Thread-Safe Interface fits very well when the critical sections are just objects.
Dealing with Mutation: Thread-Safe Interface
by Rainer Grimm
From the article:
The naive idea to protect all member functions of a class with a lock causes, in the best case, a performance issue and, in the worst case, a deadlock.
A Deadlock
The small code snippet has a deadlock.
struct Critical{ void memberFunction1(){ lock(mut); memberFunction2(); ... } void memberFunction2(){ lock(mut); ... } mutex mut; }; Critical crit; crit.memberFunction1();Calling
crit.memberFunction1causes the mutexmutto be locked twice. For simplicity reasons, the lock is a scoped lock. Here are the two issues:

Fold expressions exist in C++ since C++17 and significantly affect how we treat variadic templates. Back in the day, I wrote about
Registration is now open for CppCon 2023! The conference starts on October 1 and will be held
Locking is a straightforward idea to protect a critical section. A critical section is a section of code that, at most, one thread can use at any time.
Registration is now open for CppCon 2023! The conference starts on October 1 and will be held
C++ allows us to declare various forms of non-local objects: they usually live throughout the execution of the whole program. In this article, we’ll look at global variables, dynamic, and thread-local objects. We’ll also consider new features for safe initialization C++20.
Registration is now open for CppCon 2023! The conference starts on October 1 and will be held
Registration is now open for CppCon 2023! The conference starts on October 1 and will be held 