Basic multithreaded programs
In operating systems, we all know P/V operations can serve as mutual exclusion and synchronization primitives, but how they should be implemented in C++? In this article, I’ll tell you all about how to write a basic multithreaded C++ program.
Mutual exclusion primitive
A simple and classic example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <mutex>
#include <thread>
std::mutex m;
void worker1() {
while (true) {
std::scoped_lock lock(m); // Acquires lock on mutex m
// critical section
// lock will be automatically released here thanks to RAII.
}
}
void worker2() {
while (true) {
std::scoped_lock lock(m); // Same as above
// critical section
}
}
void some_func() {
// You may call them somewhere like this...
std::jthread t1(worker1);
std::jthread t2(worker2);
}
In loops, the two workers both competes for the std::mutex m. One will succeed, and the other blocks and waits for the next release.
Synchronization primitive
This is a bit harder to follow.
First, let’s see what a first attempt looks like:
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
#include <mutex>
#include <print>
#include <queue>
#include <thread>
int main()
{
std::queue<int> q;
std::mutex m;
std::jthread writer([&] {
int i{};
while (true) {
std::scoped_lock lock(m);
q.push(i++);
}
});
std::jthread reader([&] {
while (true) {
int t;
{
std::scoped_lock lock(m);
// Performance issue here: if most of the time the queue is empty,
// the reader will spin and waste CPU cycles.
if (q.empty()) {
continue;
}
t = q.front();
q.pop();
}
std::println("Read: {}", t);
}
});
}
It’s a correct program, but inefficient if most of the time the queue is empty. So we need a notification mechanism: only when the queue contains some data, the reader reads them from it. Consider the following 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
#include <condition_variable>
#include <mutex>
#include <print>
#include <queue>
#include <thread>
int main()
{
std::condition_variable cv;
std::mutex m;
std::queue<int> q;
std::jthread writer([&] {
int i{};
while (true) {
{
std::scoped_lock lock(m);
q.push(i++);
}
cv.notify_one();
}
});
std::jthread reader([&] {
while (true) {
int t;
{
std::unique_lock lock(m);
cv.wait(lock, [&] { return !q.empty(); }); // Wait until q is not empty.
t = q.front();
q.pop();
}
std::println("Read: {}", t);
}
});
}
But why does cv.wait need a lock, and how they work together? This remains to be solved next time.