GCC Code Coverage Report


Directory: ./
File: demos/cond_var.cpp
Date: 2022-06-30 06:29:57
Exec Total Coverage
Lines: 0 73 0.0%
Branches: 0 64 0.0%

Line Branch Exec Source
1 #include <unistd.h>
2 #include <atomic>
3 #include <condition_variable>
4 #include <iostream>
5 #include <mutex>
6 #include <thread>
7 #include <vector>
8
9 class Config
10 {
11 public:
12 std::vector<int> *vector;
13 std::atomic<bool> *running;
14 int value;
15 std::string message;
16 std::condition_variable *condition;
17 std::mutex *mutex;
18 };
19
20 void *update_vector_async(void *config_)
21 {
22 Config *config = static_cast<Config *>(config_);
23
24 while (*config->running)
25 {
26 config->mutex->lock();
27
28 std::cout << config->message;
29
30 for (unsigned int i = 0; i < config->vector->size(); i++)
31 {
32 (*config->vector)[i] = config->value;
33 }
34
35 usleep(500);
36
37 for (unsigned int i = 0; i < config->vector->size(); i++)
38 {
39 if ((*config->vector)[i] != config->value)
40 {
41 std::cout << "--- damn, the vector was not locked !\n";
42 break;
43 }
44 if (i < 10)
45 {
46 std::cout << " " << (*config->vector)[i];
47 }
48 }
49 std::cout << "\n";
50
51 config->mutex->unlock();
52
53 usleep(10);
54 }
55
56 return nullptr;
57 }
58
59 void *update_vector(void *config_)
60 {
61 Config *config = static_cast<Config *>(config_);
62
63 while (*config->running)
64 {
65 {
66 std::unique_lock<std::mutex> lock(*config->mutex);
67 config->condition->wait(lock);
68
69 std::cout << config->message;
70 for (unsigned int i = 0; i < config->vector->size(); i++)
71 {
72 (*config->vector)[i] = config->value;
73 }
74
75 usleep(500);
76
77 for (unsigned int i = 0; i < config->vector->size(); i++)
78 {
79 if ((*config->vector)[i] != config->value)
80 {
81 std::cout << "\n--- damn, the vector was not locked !\n";
82 break;
83 }
84 if (i < 10)
85 {
86 std::cout << " " << (*config->vector)[i];
87 }
88 }
89 std::cout << "\n";
90 }
91
92 config->condition->notify_one();
93 }
94
95 return nullptr;
96 }
97
98 int main()
99 {
100 std::vector<int> v(1000000);
101 std::atomic<bool> running(true);
102 std::condition_variable condition;
103 std::mutex mutex;
104
105 Config config1;
106 config1.value = 1;
107 config1.vector = &v;
108 config1.running = &running;
109 config1.mutex = &mutex;
110 config1.message = std::string("ping");
111 config1.condition = &condition;
112 std::thread thread1(update_vector, &config1);
113
114 Config config2;
115 config2.value = 2;
116 config2.vector = &v;
117 config2.running = &running;
118 config2.mutex = &mutex;
119 config2.condition = &condition;
120 config2.message = std::string("pong");
121 std::thread thread2(update_vector, &config2);
122
123 Config config3;
124 config3.value = 3;
125 config3.vector = &v;
126 config3.running = &running;
127 config3.mutex = &mutex;
128 config3.message = std::string("pung");
129 std::thread thread3(update_vector_async, &config3);
130
131 usleep(1000);
132 condition.notify_one();
133 usleep(10000000);
134
135 std::cout << "\tSTOPPING!\n";
136
137 running = false;
138 thread1.join();
139 thread2.join();
140 thread3.join();
141 }
142