-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThreadPool.cpp
128 lines (107 loc) · 3.57 KB
/
ThreadPool.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
/*************************************************************************
> File Name: threadpool.h
> Author: Weidong, ZHANG
> Mail: [email protected]
> Created Time: Thu 04 Dec 2014 08:55:22 PM PST
************************************************************************/
#include <stdlib.h>
#include "ThreadPool.h"
using namespace std;
pthread_mutex_t ThreadPool::mutexSync = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t ThreadPool::mutexWorkCompletion = PTHREAD_MUTEX_INITIALIZER;
ThreadPool::ThreadPool()
{
ThreadPool(2);
}
ThreadPool::ThreadPool(int maxThreads)
{
if (maxThreads < 1) maxThreads=1;
//mutexSync = PTHREAD_MUTEX_INITIALIZER;
//mutexWorkCompletion = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_lock(&mutexSync);
this->maxThreads = maxThreads;
this->queueSize = maxThreads;
//workerQueue = new WorkerThread *[maxThreads];
workerQueue.resize(maxThreads, NULL);
topIndex = 0;
bottomIndex = 0;
incompleteWork = 0;
sem_init(&availableWork, 0, 0);
sem_init(&availableThreads, 0, queueSize);
pthread_mutex_unlock(&mutexSync);
}
void ThreadPool::initializeThreads()
{
for(int i = 0; i < maxThreads; ++i)
{
pthread_t tempThread;
pthread_create(&tempThread, NULL, &ThreadPool::threadExecute, (void *) this );
//threadIdVec[i] = tempThread;
}
}
ThreadPool::~ThreadPool()
{
workerQueue.clear();
}
void ThreadPool::destroyPool(int maxPollSecs = 2)
{
while(incompleteWork > 0 )
{
//cout << "Work is still incomplete=" << incompleteWork << endl;
sleep(maxPollSecs);
}
cout << "All Done!! Wow! That was a lot of work!" << endl;
sem_destroy(&availableWork);
sem_destroy(&availableThreads);
pthread_mutex_destroy(&mutexSync);
pthread_mutex_destroy(&mutexWorkCompletion);
}
bool ThreadPool::assignWork(WorkerThread *workerThread)
{
pthread_mutex_lock(&mutexWorkCompletion);
incompleteWork++;
//cout << "assignWork...incomapleteWork=" << incompleteWork << endl;
pthread_mutex_unlock(&mutexWorkCompletion);
sem_wait(&availableThreads);
pthread_mutex_lock(&mutexSync);
//workerVec[topIndex] = workerThread;
workerQueue[topIndex] = workerThread;
//cout << "Assigning Worker[" << workerThread->id << "] Address:[" << workerThread << "] to Queue index [" << topIndex << "]" << endl;
if(queueSize !=1 )
topIndex = (topIndex+1) % (queueSize-1);
sem_post(&availableWork);
pthread_mutex_unlock(&mutexSync);
return true;
}
bool ThreadPool::fetchWork(WorkerThread **workerArg)
{
sem_wait(&availableWork);
pthread_mutex_lock(&mutexSync);
WorkerThread * workerThread = workerQueue[bottomIndex];
workerQueue[bottomIndex] = NULL;
*workerArg = workerThread;
if(queueSize !=1 )
bottomIndex = (bottomIndex+1) % (queueSize-1);
sem_post(&availableThreads);
pthread_mutex_unlock(&mutexSync);
return true;
}
void *ThreadPool::threadExecute(void *param)
{
WorkerThread *worker = NULL;
while(((ThreadPool *)param)->fetchWork(&worker))
{
if(worker)
{
worker->executeThis();
//cout << "worker[" << worker->id << "]\tdelete address: [" << worker << "]" << endl;
delete worker;
worker = NULL;
}
pthread_mutex_lock( &(((ThreadPool *)param)->mutexWorkCompletion) );
//cout << "Thread " << pthread_self() << " has completed a Job !" << endl;
((ThreadPool *)param)->incompleteWork--;
pthread_mutex_unlock( &(((ThreadPool *)param)->mutexWorkCompletion) );
}
return 0;
}