-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathExercise_02_CarParking.cpp
More file actions
259 lines (192 loc) · 6.81 KB
/
Exercise_02_CarParking.cpp
File metadata and controls
259 lines (192 loc) · 6.81 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// ===========================================================================
// Exercise_02_CarParking.cpp - Car Parking Exercise
// ===========================================================================
#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>
#include <cstdlib>
#ifdef _DEBUG
#ifndef DBG_NEW
#define DBG_NEW new ( _NORMAL_BLOCK , __FILE__ , __LINE__ )
#define new DBG_NEW
#endif
#endif // _DEBUG
#include "../Logger/Logger.h"
#include <chrono> // for std::chrono::milliseconds
#include <condition_variable> // for std::condition_variable
#include <iostream> // for std::cout
#include <memory> // for std::unique_lock
#include <mutex> // for std::mutex
#include <random> // for std::random_device
#include <semaphore> // for std::counting_semaphore
#include <stop_token> // for std::stop_token
#include <thread> // for std::jthread, std::thread::id
#include <vector> // for std::vector
// ===========================================================================
constexpr std::size_t NumParkingLots = 3;
namespace Car_Parking {
struct ParkingArea
{
virtual ~ParkingArea() = default;
virtual void enter() = 0;
virtual void leave() = 0;
};
class FirstParkingArea : public ParkingArea
{
private:
std::counting_semaphore<> m_emptyLots;
public:
FirstParkingArea()
: m_emptyLots{ NumParkingLots }
{
Logger::log(std::cout, "FirstParkingArea has ", NumParkingLots, " empty lots.");
}
void enter() {
m_emptyLots.acquire();
}
void leave() {
m_emptyLots.release();
}
};
class AnotherParkingArea : public ParkingArea
{
private:
std::mutex m_mutex;
std::condition_variable m_condition;
std::size_t m_emptyLots;
public:
AnotherParkingArea()
: m_emptyLots{ NumParkingLots }
{
Logger::log(std::cout, "AnotherParkingArea has ", NumParkingLots, " empty lots.");
}
void enter() {
{
std::unique_lock<std::mutex> guard{ m_mutex };
m_condition.wait(
guard,
[this]() {
return m_emptyLots != 0;
}
);
--m_emptyLots;
}
}
void leave() {
{
std::lock_guard<std::mutex> guard{ m_mutex };
++m_emptyLots;
}
m_condition.notify_one();
}
};
class Car
{
private:
std::random_device& m_device;
std::thread::id m_tid;
ParkingArea& m_parkingArea;
std::size_t m_id;
public:
Car(std::random_device& device, ParkingArea& parkingArea, std::size_t id)
: m_device{ device }, m_parkingArea{ parkingArea }, m_id{ id }
{
}
void driving(std::stop_token token) {
m_tid = std::this_thread::get_id();
while (!token.stop_requested())
{
Logger::log(std::cout, "Car ", m_id, " is driving");
// random value from 0 up to 3000
std::random_device::result_type offset{ m_device() % 3000 };
std::this_thread::sleep_for(std::chrono::milliseconds{ 3000ll + offset });
Logger::log(std::cout, "Want to park car ", m_id, " now");
m_parkingArea.enter();
Logger::log(std::cout, "Car ", m_id, " is parking now!");
std::this_thread::sleep_for(std::chrono::milliseconds{ 2000 });
Logger::log(std::cout, "Want to leave parking area with car ", m_id);
m_parkingArea.leave();
Logger::log(std::cout, "Car ", m_id, " has left parking area");
}
Logger::log(std::cout, "Car ", m_id, " finished driving!");
}
};
class TrafficSimulation
{
private:
ParkingArea& m_parkingArea;
std::vector<std::jthread> m_threads;
std::vector<std::unique_ptr<Car>> m_cars;
std::random_device m_device;
static std::size_t s_nextId;
public:
TrafficSimulation(ParkingArea& parkingArea)
: m_parkingArea{ parkingArea }
{}
void addCar() {
s_nextId++;
auto car{ std::make_unique<Car>(m_device, m_parkingArea, s_nextId) };
Car* carPtr = car.get();
// store car first so ownership and storage are stable
m_cars.push_back(std::move(car));
// Create a jthread that calls the car's driving method.
// Note: Capture raw pointer (stable while car is owned by m_cars).
m_threads.emplace_back([carPtr](std::stop_token st) {
carPtr->driving(st);
});
}
void startSimulation() {
Logger::log(std::cout, "Starting Simulation:");
}
void stopSimulation() {
Logger::log(std::cout, "Issuing Stop Requests ...");
for (auto& thread : m_threads) {
thread.request_stop();
}
for (auto& thread : m_threads) {
if (thread.joinable()) {
thread.join();
}
}
Logger::log(std::cout, "Stopped Simulation.");
}
};
std::size_t TrafficSimulation::s_nextId{};
}
static void carParking() {
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
using namespace Car_Parking;
FirstParkingArea m_parkingArea;
TrafficSimulation simulation{ m_parkingArea };
simulation.startSimulation();
simulation.addCar();
simulation.addCar();
simulation.addCar();
simulation.addCar();
simulation.addCar();
simulation.addCar();
std::this_thread::sleep_for(std::chrono::milliseconds{ 20000 });
simulation.stopSimulation();
}
static void anotherCarParking() {
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
using namespace Car_Parking;
AnotherParkingArea m_parkingArea;
TrafficSimulation simulation{ m_parkingArea };
simulation.startSimulation();
simulation.addCar();
simulation.addCar();
simulation.addCar();
simulation.addCar();
simulation.addCar();
simulation.addCar();
std::this_thread::sleep_for(std::chrono::milliseconds{ 20000 });
simulation.stopSimulation();
}
void exercise_car_parking()
{
carParking();
anotherCarParking();
}
// ===========================================================================
// End-of-File
// ===========================================================================