-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThreadPool.cpp
148 lines (117 loc) · 2.7 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include "ThreadPool.h"
namespace ThreadPool
{
#ifndef WIN32
pthread_mutex_t ThreadPool::mutexSync = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t ThreadPool::mutexTaskCompletion = PTHREAD_MUTEX_INITIALIZER;
#endif
ThreadPool::ThreadPool()
{
ThreadPool(2);
}
ThreadPool::ThreadPool(int maxThreads)
{
if (maxThreads < 1)
maxThreads = 1;
#ifndef WIN32
pthread_mutex_lock(&mutexSync);
#endif
this->maxThreads = maxThreads;
this->queueSize = maxThreads+2;
incompleteTask = 0;
#ifndef WIN32
availableTask=new IlmThread::Semaphore(0);
availableThreads=new IlmThread::Semaphore(maxThreads);
#endif
initializeThreads();
#ifndef WIN32
pthread_mutex_unlock(&mutexSync);
#endif
}
void ThreadPool::initializeThreads()
{
for (int i = 0; i < maxThreads; ++i)
{
#ifndef WIN32
pthread_t tempThread;
pthread_create(&tempThread, NULL, &ThreadPool::threadExecute,(void *) this);
threadIdVec[i] = tempThread;
#endif
}
}
ThreadPool::~ThreadPool()
{
destroyPool();
}
void ThreadPool::destroyPool(int maxPollSecs)
{
#ifndef WIN32
for(int i=0;i<maxThreads;i++)
{
pthread_join(threadIdVec[i],NULL);
}
delete availableTask;
delete availableThreads;
pthread_mutex_destroy(&mutexSync);
pthread_mutex_destroy(&mutexTaskCompletion);
#endif
}
bool ThreadPool::assignTask(Task task/*unsigned int (*task)()*/,void *argument)
{
#ifndef WIN32
pthread_mutex_lock(&mutexTaskCompletion);
incompleteTask++;
pthread_mutex_unlock(&mutexTaskCompletion);
availableThreads->wait();
pthread_mutex_lock(&mutexSync);
Work work;
work.task=task;
work.argument=argument;
taskQueue.push_back(work);
availableTask->post();
pthread_mutex_unlock(&mutexSync);
#endif
return true;
}
bool ThreadPool::fetchTask(Work *w/*unsigned int (**task)()*/)
{
#ifndef WIN32
availableTask->wait();
pthread_mutex_lock(&mutexSync);
if(!taskQueue.empty())
{
Work work=taskQueue.front();
w->task=work.task;
w->argument=work.argument;
taskQueue.erase(taskQueue.begin());
}
else
{
pthread_mutex_unlock(&mutexSync);
return false;
}
availableThreads->post();
pthread_mutex_unlock(&mutexSync);
#endif
return true;
}
#ifndef WIN32
void *ThreadPool::threadExecute(void *param)
{
ThreadPool *tp=(ThreadPool*)param;
while (1)
{
Work tasker;
if(!(tp->fetchTask(&tasker)))
{
continue;
}
(tasker.task)(tasker.argument);
pthread_mutex_lock(&(((ThreadPool *) param)->mutexTaskCompletion));
((ThreadPool *) param)->incompleteTask--;
pthread_mutex_unlock(&(((ThreadPool *) param)->mutexTaskCompletion));
}
return (void*)0;
}
#endif
}/*ThreadPool*/