-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThreadLibrary.cpp
168 lines (137 loc) · 4.11 KB
/
ThreadLibrary.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
#include "ThreadLibrary.h"
#include <iostream>
// Create and run threads
void ThreadLibrary::createAndRunThreads(int numThreads, std::function<void(int)> task) {
totalThreads = numThreads;
completedThreads = 0;
allThreadsCompleted = false; // Reset flag
for (int i = 0; i < numThreads; ++i) {
auto data = std::make_unique<ThreadData>();
data->threadId = i;
data->task = task;
data->library = this;
pthread_t thread;
int rc = pthread_create(&thread, nullptr, ThreadLibrary::threadFunction, data.get());
if (rc) {
std::cerr << "Error: unable to create thread, " << rc << std::endl;
exit(-1);
}
threads.push_back(thread);
threadData.push_back(std::move(data));
}
}
// Thread function
void* ThreadLibrary::threadFunction(void* arg) {
ThreadData* data = static_cast<ThreadData*>(arg);
data->task(data->threadId);
data->library->signalThreadCompletion();
return nullptr;
}
// Signal thread completion
void ThreadLibrary::signalThreadCompletion() {
std::unique_lock<std::mutex> lock(mtx);
completedThreads++;
if (completedThreads == totalThreads) {
allThreadsCompleted = true;
cv.notify_all();
}
}
// Join all threads
void ThreadLibrary::joinAllThreads() {
std::unique_lock<std::mutex> lock(mtx);
cv.wait(lock, [this] { return allThreadsCompleted; });
for (auto& thread : threads) {
pthread_join(thread, nullptr);
}
threads.clear();
threadData.clear();
}
// Mutex implementation
ThreadLibrary::Mutex::Mutex() {
pthread_mutex_init(&mutex, nullptr);
}
ThreadLibrary::Mutex::~Mutex() {
pthread_mutex_destroy(&mutex);
}
void ThreadLibrary::Mutex::lock() {
pthread_mutex_lock(&mutex);
}
void ThreadLibrary::Mutex::unlock() {
pthread_mutex_unlock(&mutex);
}
pthread_mutex_t* ThreadLibrary::Mutex::getPthreadMutex() {
return &mutex;
}
// LockGuard implementation
ThreadLibrary::LockGuard::LockGuard(Mutex& m) : mutex(m) {
mutex.lock();
}
ThreadLibrary::LockGuard::~LockGuard() {
mutex.unlock();
}
// ConditionVariable implementation
ThreadLibrary::ConditionVariable::ConditionVariable() {
pthread_cond_init(&cond, nullptr);
}
ThreadLibrary::ConditionVariable::~ConditionVariable() {
pthread_cond_destroy(&cond);
}
void ThreadLibrary::ConditionVariable::wait(Mutex& m) {
pthread_cond_wait(&cond, m.getPthreadMutex());
}
void ThreadLibrary::ConditionVariable::notify_one() {
pthread_cond_signal(&cond);
}
void ThreadLibrary::ConditionVariable::notify_all() {
pthread_cond_broadcast(&cond);
}
// ThreadAttributes implementation
ThreadLibrary::ThreadAttributes::ThreadAttributes() {
pthread_attr_init(&attr);
}
ThreadLibrary::ThreadAttributes::~ThreadAttributes() {
pthread_attr_destroy(&attr);
}
void ThreadLibrary::ThreadAttributes::setStackSize(size_t size) {
pthread_attr_setstacksize(&attr, size);
}
void ThreadLibrary::ThreadAttributes::setDetachState(bool detached) {
pthread_attr_setdetachstate(&attr, detached ? PTHREAD_CREATE_DETACHED : PTHREAD_CREATE_JOINABLE);
}
pthread_attr_t* ThreadLibrary::ThreadAttributes::getPthreadAttr() {
return &attr;
}
// ThreadLocal implementation
template <typename T>
ThreadLibrary::ThreadLocal<T>::ThreadLocal() {
pthread_key_create(&key, destructor);
}
template <typename T>
ThreadLibrary::ThreadLocal<T>::~ThreadLocal() {
pthread_key_delete(key);
}
template <typename T>
T& ThreadLibrary::ThreadLocal<T>::get() {
T* value = static_cast<T*>(pthread_getspecific(key));
if (!value) {
value = new T();
pthread_setspecific(key, value);
}
return *value;
}
template <typename T>
void ThreadLibrary::ThreadLocal<T>::set(const T& value) {
T* ptr = static_cast<T*>(pthread_getspecific(key));
if (!ptr) {
ptr = new T(value);
pthread_setspecific(key, ptr);
} else {
*ptr = value;
}
}
template <typename T>
void ThreadLibrary::ThreadLocal<T>::destructor(void* value) {
delete static_cast<T*>(value);
}
// Explicit template instantiation for ThreadLocal<int>
template class ThreadLibrary::ThreadLocal<int>;