-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconcurrent_programming_examples.cpp
305 lines (252 loc) · 7.42 KB
/
concurrent_programming_examples.cpp
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#include <iostream>
#include <thread>
#include <fstream>
#include <string>
#include <exception>
// Example 1: Basic Thread Usage
// This example demonstrates how to create a thread and
// wait for it to finish using the join() method.
void printMessage() {
// This function is called by the thread and prints
// a message to the console.
cout << "Hello, World!" << endl;
}
int main() {
// Create a thread and pass the printMessage function as
// its argument.
std::thread t1(printMessage);
// Wait for the thread to finish using the join() method.
// This will block the main thread until the thread
// has finished.
t1.join();
// The thread has finished, so the main thread can
// continue executing.
return 0;
}
// Example 2: Race Condition
// This example demonstrates a race condition, which
// occurs when two threads access shared resources and
// one of the threads modifies the resource without the
// other thread knowing about it.
class FileWriter {
ofstream& fileStream;
public:
// Constructor that takes a reference to an ofstream
// object as its argument.
FileWriter(ofstream& fs) : fileStream(fs) {}
// Operator() is called by the thread and writes
// data to the file.
void operator()() {
for (int i = 0; i > -100; --i) {
// Write data to the file, but do not
// synchronize with the other thread.
fileStream << "From thread: " << i << endl;
}
}
};
int main() {
// Create a file stream object and open the file.
ofstream logFile("log.txt");
// Create a thread and pass the FileWriter object
// as its argument.
FileWriter writer(logFile);
std::thread t1(writer);
// The main thread also writes data to the file,
// but does not synchronize with the other thread.
for (int i = 0; i < 100; ++i) {
logFile << "From main: " << i << endl;
}
// The main thread has finished writing data,
// so wait for the thread to finish using the
// join() method.
t1.join();
// Close the file stream.
logFile.close();
return 0;
}
// Example 3: Handling Exceptions
// This example demonstrates how to handle exceptions
// when working with threads.
int main() {
// Create a file stream object and open the file.
ofstream logFile("log.txt");
// Create a thread and pass the FileWriter object
// as its argument.
FileWriter writer(logFile);
std::thread t1(writer);
try {
// The main thread writes data to the file,
// but may throw an exception.
for (int i = 0; i < 100; ++i) {
logFile << "From main: " << i << endl;
// Simulate an exception.
if (i == 50) {
throw std::runtime_error("Simulated exception");
}
}
} catch (...) {
// If an exception occurs, wait for the thread
// to finish using the join() method and then
// rethrow the exception.
t1.join();
throw;
}
// If no exception occurs, wait for the thread
// to finish using the join() method.
t1.join();
// Close the file stream.
logFile.close();
return 0;
}
// Example 4: RAII for Thread Management
// This example demonstrates how to use RAII (Resource
// Acquisition Is Initialization) to manage threads.
class ThreadGuard {
std::thread& t;
public:
// Constructor that takes a reference to a thread
// object as its argument.
explicit ThreadGuard(std::thread& t_) : t(t_) {}
// Destructor that waits for the thread to finish
// using the join() method.
~ThreadGuard() {
if (t.joinable()) {
t.join();
}
}
};
int main() {
// Create a file stream object and open the file.
ofstream logFile("log.txt");
// Create a thread and pass the FileWriter object
// as its argument.
FileWriter writer(logFile);
std::thread t1(writer);
// Create a ThreadGuard object and pass the thread
// object as its argument. The destructor of the
// ThreadGuard object will wait for the thread to
// finish using the join() method.
ThreadGuard guard(t1);
// The main thread writes data to the file.
for (int i = 0; i < 100; ++i) {
logFile << "From main: " << i << endl;
}
// The destructor of the ThreadGuard object will
// wait for the thread to finish.
logFile.close();
return 0;
}
// Example 5: Passing Parameters to Threads
// This example demonstrates how to pass parameters
// to threads using std::ref.
void modifyMessage(string& msg) {
// Modify the message.
msg = "Beauty is only skin-deep";
cout << "Thread says: " << msg << endl;
}
int main() {
// Create a string object and initialize it.
string message = "A friend in need is a friend indeed.";
// Create a thread and pass the modifyMessage
// function and the string object as its arguments.
std::thread t1(modifyMessage, std::ref(message));
// Wait for the thread to finish using the join()
// method.
t1.join();
// Print the modified message.
cout << "Main says: " << message << endl;
return 0;
}
// Example 6: Passing Class Methods to Threads
// This example demonstrates how to pass class methods
// to threads.
class MessageModifier {
public:
void modifyMessage(string* msg) {
// Modify the message.
*msg = "Beauty is only skin-deep";
cout << "Thread says: " << *msg << endl;
}
};
int main() {
// Create a string object and initialize it.
string message = "A friend in need is a friend indeed.";
// Create an object of the MessageModifier class.
MessageModifier modifier;
// Create a thread and pass the modifyMessage
// method of the MessageModifier class as its
// argument.
std::thread t1(&MessageModifier::modifyMessage, &modifier, &message);
// Detach the thread from the main thread.
t1.detach();
// Print the modified message.
cout << "Main says: " << message << endl;
return 0;
}
// Example 7: Moving Parameters to Threads
// This example demonstrates how to move parameters
// to threads using std::move.
void printMessage(string msg) {
// Print the message.
cout << "Thread says: " << msg << endl;
}
int main() {
// Create a string object and initialize it.
string* messagePtr = new string("A friend in need is a friend indeed.");
// Create a thread and pass the printMessage
// function and the string object as its arguments.
std::thread t1(printMessage, std::move(*messagePtr));
// Wait for the thread to finish using the join()
// method.
t1.join();
// Print the modified message.
cout << "Main: " << *messagePtr << endl;
// Delete the string object.
delete messagePtr;
return 0;
}
// Example 8: Moving Thread Objects
// This example demonstrates how to move thread objects
// using std::move.
void printMessage() {
// Print a message.
cout << "Thread says: Hello, World!" << endl;
}
int main() {
// Create a thread and pass the printMessage
// function as its argument.
std::thread t1(printMessage);
// Move the thread object to another thread.
std::thread t2 = std::move(t1);
// Wait for the thread to finish using the join()
// method.
t2.join();
return 0;
}
// Example 9: RAII with Move Semantics
// This example demonstrates how to use RAII (Resource
// Acquisition Is Initialization) with move semantics
// to manage threads.
class ThreadGuard {
std::thread t;
public:
// Constructor that takes a thread object as its
// argument using move semantics.
explicit ThreadGuard(std::thread t_) : t(std::move(t_)) {}
// Destructor that waits for the thread to finish
// using the join() method.
~ThreadGuard() {
if (t.joinable()) {
t.join();
}
}
};
int main() {
// Create a thread and pass the printMessage
// function as its argument.
std::thread t1(printMessage);
// Create a ThreadGuard object and pass the thread
// object as its argument using move semantics.
ThreadGuard guard(std::move(t1));
return 0;
}