With C++14 came reader-writer locks. The idea is straightforward and promising. Arbitrary reading threads can access the critical region at the same time, but only one thread is allowed to write.
Minimized bottleneck
Reader-writer locks do not solve the fundamental problem - threads competing for access to a critical region. But reader-writer locks help a lot - to minimize the bottleneck. Let's have an example.
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
// readerWriterLock.cpp
#include <iostream>
#include <map>
#include <shared_mutex>
#include <string>
#include <thread>
std::map<std::string,int> teleBook{{"Dijkstra",1972},{"Scott",1976},{"Ritchie",1983}};
std::shared_timed_mutex teleBookMutex;
void addToTeleBook(const std::string& na, int tele){
std::lock_guard<std::shared_timed_mutex> writerLock(teleBookMutex);
std::cout << "\nSTARTING UPDATE " << na;
std::this_thread::sleep_for(std::chrono::milliseconds(500));
teleBook[na]= tele;
std::cout << " ... ENDING UPDATE " << na << std::endl;
}
void printNumber(const std::string& na){
std::shared_lock<std::shared_timed_mutex> readerLock(teleBookMutex);
std::cout << na << ": " << teleBook[na];
}
int main(){
std::cout << std::endl;
std::thread reader1([]{ printNumber("Scott"); });
std::thread reader2([]{ printNumber("Ritchie"); });
std::thread w1([]{ addToTeleBook("Scott",1968); });
std::thread reader3([]{ printNumber("Dijkstra"); });
std::thread reader4([]{ printNumber("Scott"); });
std::thread w2([]{ addToTeleBook("Bjarne",1965); });
std::thread reader5([]{ printNumber("Scott"); });
std::thread reader6([]{ printNumber("Ritchie"); });
std::thread reader7([]{ printNumber("Scott"); });
std::thread reader8([]{ printNumber("Bjarne"); });
reader1.join();
reader2.join();
reader3.join();
reader4.join();
reader5.join();
reader6.join();
reader7.join();
reader8.join();
w1.join();
w2.join();
std::cout << std::endl;
std::cout << "\nThe new telephone book" << std::endl;
for (auto teleIt: teleBook){
std::cout << teleIt.first << ": " << teleIt.second << std::endl;
}
std::cout << std::endl;
}
|
The telebook in line 9 is the shared variable, which has to be protected. Eight threads want to read the telephone book, two threads want to modify (lines 30 - 39). To access the telephone book at the same time, the reading threads use the std::shared_lock<std::shared_timed_mutex>> in line 22. This is in opposition to the writing threads, which need exclusive access to the critical section. The exclusivity is given by the std::lock_guard<std::shared_timed_mutex>> in line 14. In the end, the program displays (lines 54 - 57) the updated telephone book.

The screenshot shows that the output of the reading threads overlaps, while the writing thread is executed one after the other. It means that the reading operations are performed at the same time.
That was easy. Too easy.
Undefined behavior
The program has undefined behavior. What? Before you continue, stop for a few seconds and think. By the way, the concurrent access of std::cout is not the issue.
The characteristic of a race condition is, that at least two threads access the shared variable at the same time, at least one of them is a writer. Exactly what is happening in the program. A characteristic of the ordered associative container is that the reading of the container can modify it. It happens if the element is not available in the container - the case "Bjarne". If "Bjarne" is not found in the telephone book, a pair ("Bjarne",0) will be created from the read operation. For the details, have a look at cppreference.com.
What's next?
In the next post, I continue with the thread-safe initialization of data in multithreading programs.
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, Eduardo Velasquez, 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, Ralf Holly, Juan Dent, George Liao, and Daniel Ceperley.
Thanks in particular to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, Rusty Fleming, Ralf Abramowitsch, John Nebel, and Mipko.
My special thanks to Embarcadero 
My special thanks to PVS-Studio 
Mentoring Program
My new mentoring program "Fundamentals for C++ Professionals" starts on the 22nd of April. Get more information here: https://bit.ly/MentoringProgramModernesCpp.
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
- Phone: +49 7472 917441
- Mobil:: +49 176 5506 5086
- Mail: This email address is being protected from spambots. You need JavaScript enabled to view it.
- German Seminar Page: www.ModernesCpp.de
- English Seminar Page: www.ModernesCpp.net
Modernes C++,

Comments
D.
I must spend some time studying moгe or figuring outt mⲟre.
Thank you for wonderful informatіοn I waѕ on thᥱ lookout for thіs іnformation for my mission.
RSS feed for comments to this post