-
Notifications
You must be signed in to change notification settings - Fork 0
/
30-thread.cc
92 lines (72 loc) · 2.15 KB
/
30-thread.cc
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//
// Program
// Creates two threads which waits on a condition
// variable sent by another function.
//
// Compile
// g++ -Wall -Wextra -pedantic -std=c++17 -pthread -o 30-thread 30-thread.cc
//
// Execution
// ./30-thread
//
#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <chrono>
using namespace std::chrono_literals;
//
// Data used by the example
//
struct Data {
Data():mutex(), cond() {}
std::mutex mutex;
std::condition_variable cond;
};
//
// Function to be called for thread
//
static void thread_callback_1(Data& data) {
auto id = std::this_thread::get_id();
std::cout << __func__ << " is assigned to thread id # " << id << '\n';
std::cout << "Thread id # " << id << " is locking mutex to wait on condition" << '\n';
std::unique_lock<std::mutex> lock(data.mutex);
data.cond.wait(lock);
std::cout << "Thread id # " << id << " is having condition signal..." << '\n';
for (int i=1; i < 5; ++i) {
std::this_thread::sleep_for(1s);
}
std::cout << "Thread id # " << id << " is complete" << '\n';
}
//
// Function to be called for thread
//
static void thread_callback_2(Data& data) {
auto id = std::this_thread::get_id();
std::cout << __func__ << " is assigned to thread id # " << id << '\n';
std::cout << "Thread id # " << id << " is locking mutex to wait on condition" << '\n';
std::unique_lock<std::mutex> lock(data.mutex);
data.cond.wait(lock);
std::cout << "Thread id # " << id << " is having condition signal..." << '\n';
for (int i=1; i < 5; ++i) {
std::this_thread::sleep_for(1s);
}
std::cout << "Thread id # " << id << " is complete" << '\n';
}
//
// Entry function
//
int main() {
std::cout << "--- Condition variable ---" << '\n';
Data data;
std::thread t1(thread_callback_1, std::ref(data));
std::thread t2(thread_callback_2, std::ref(data));
std::cout << "main is sleeping for 4s..." << '\n';
std::this_thread::sleep_for(4s);
std::cout << "Sending notification to threads..." << '\n';
data.cond.notify_all(); // send notification to all threads waiting on cv
std::cout << "Waiting for threads..." << '\n';
t1.join();
t2.join();
return 0;
}