-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample3.cpp
43 lines (34 loc) · 888 Bytes
/
example3.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
#include "ThreadLibrary.h"
#include <iostream>
#include <thread>
#include <vector>
#include <functional>
ThreadLibrary::Mutex mtx;
ThreadLibrary::ConditionVariable cv;
bool ready = false;
void waitFunction(int threadId) {
ThreadLibrary::LockGuard lock(mtx);
while (!ready) {
cv.wait(mtx);
}
std::cout << "Thread " << threadId << " proceeding after notification." << std::endl;
}
void notifyFunction() {
{
ThreadLibrary::LockGuard lock(mtx);
ready = true;
}
cv.notify_all();
}
int main() {
ThreadLibrary threadLib;
// Create and run threads
threadLib.createAndRunThreads(5, waitFunction);
// Simulate some work in main thread
std::this_thread::sleep_for(std::chrono::seconds(1));
// Notify all waiting threads
notifyFunction();
// Join all threads
threadLib.joinAllThreads();
return 0;
}