29 typedef void *(*ThreadFunc)(
void *);
37 unsigned virtual executeThis()
43 WorkerThread(ThreadFunc job,
void *arg): job_func(job), job_arg(arg)
67 void destroyPool(
int maxPollMilliSecs=100);
68 void finishAll(
int maxPollMilliSecs=100);
70 bool assignWork(WorkerThread *worker);
71 bool fetchWork(WorkerThread **worker);
73 void initializeThreads();
75 static void *threadExecute(
void *param);
77 static pthread_mutex_t mutexSync;
78 static pthread_mutex_t mutexWorkCompletion;
84 pthread_cond_t condCrit;
86 sem_t availableThreads;
88 std::vector<WorkerThread *> workerQueue;
102 pthread_mutex_t ThreadPool::mutexSync = PTHREAD_MUTEX_INITIALIZER;
103 pthread_mutex_t ThreadPool::mutexWorkCompletion = PTHREAD_MUTEX_INITIALIZER;
106 ThreadPool::ThreadPool()
125 ThreadPool::ThreadPool(
int maxThreads)
127 if (maxThreads < 1) maxThreads=1;
132 pthread_mutex_lock(&mutexSync);
133 this->maxThreads = maxThreads;
134 this->queueSize = maxThreads;
136 workerQueue.resize(maxThreads, NULL);
140 sem_init(&availableWork, 0, 0);
141 sem_init(&availableThreads, 0, queueSize);
142 pthread_mutex_unlock(&mutexSync);
145 void ThreadPool::initializeThreads()
147 for(
int i = 0; i<maxThreads; ++i)
149 pthread_t tempThread;
150 pthread_create(&tempThread, NULL, &ThreadPool::threadExecute, (
void *)
this );
156 ThreadPool::~ThreadPool()
162 void ThreadPool::finishAll(
int maxPollMilliSecs)
164 while( incompleteWork>0 )
167 usleep(maxPollMilliSecs);
172 void ThreadPool::destroyPool(
int maxPollMilliSecs)
174 while( incompleteWork>0 )
177 usleep(maxPollMilliSecs);
180 sem_destroy(&availableWork);
181 sem_destroy(&availableThreads);
182 pthread_mutex_destroy(&mutexSync);
183 pthread_mutex_destroy(&mutexWorkCompletion);
188 bool ThreadPool::assignWork(WorkerThread *workerThread)
191 pthread_mutex_lock(&mutexWorkCompletion);
194 pthread_mutex_unlock(&mutexWorkCompletion);
196 sem_wait(&availableThreads);
198 pthread_mutex_lock(&mutexSync);
200 workerQueue[topIndex] = workerThread;
203 topIndex = (topIndex+1) % (queueSize);
204 sem_post(&availableWork);
205 pthread_mutex_unlock(&mutexSync);
209 bool ThreadPool::fetchWork(WorkerThread **workerArg)
211 sem_wait(&availableWork);
213 pthread_mutex_lock(&mutexSync);
214 WorkerThread * workerThread = workerQueue[bottomIndex];
215 workerQueue[bottomIndex] = NULL;
216 *workerArg = workerThread;
218 bottomIndex = (bottomIndex+1) % (queueSize);
219 sem_post(&availableThreads);
220 pthread_mutex_unlock(&mutexSync);
224 void *ThreadPool::threadExecute(
void *param)
226 WorkerThread *worker = NULL;
228 while(((ThreadPool *)param)->fetchWork(&worker))
232 worker->executeThis();
237 pthread_mutex_lock( &(((ThreadPool *)param)->mutexWorkCompletion) );
239 ((ThreadPool *)param)->incompleteWork--;
240 pthread_mutex_unlock( &(((ThreadPool *)param)->mutexWorkCompletion) );
to enable thread pooling while using multiple CUDA devices. ThreadPool class manages all the ThreadPo...
Definition: thread_pool.h:60
Contains classes that help with thread management. Currently only Pthreads.