Skip to content

Commit dc033bb

Browse files
committed
Refactoring
1 parent a77bf08 commit dc033bb

File tree

2 files changed

+7
-20
lines changed

2 files changed

+7
-20
lines changed

threadpool.cpp

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,12 @@ pthread_mutex_t ThreadPool::mutexSync = PTHREAD_MUTEX_INITIALIZER;
2525
pthread_mutex_t ThreadPool::mutexWorkCompletion = PTHREAD_MUTEX_INITIALIZER;
2626

2727

28-
29-
ThreadPool::ThreadPool()
30-
{
31-
ThreadPool(2);
32-
}
33-
34-
ThreadPool::ThreadPool(int maxThreads)
28+
ThreadPool::ThreadPool(unsigned int num_thread)
29+
:_num_thread(num_thread)
30+
,workerQueue(num_thread, NULL)
31+
,queueSize(num_thread)
3532
{
36-
if (maxThreads < 1) maxThreads=1;
37-
38-
//mutexSync = PTHREAD_MUTEX_INITIALIZER;
39-
//mutexWorkCompletion = PTHREAD_MUTEX_INITIALIZER;
40-
4133
pthread_mutex_lock(&mutexSync);
42-
this->maxThreads = maxThreads;
43-
this->queueSize = maxThreads;
44-
//workerQueue = new WorkerThread *[maxThreads];
45-
workerQueue.resize(maxThreads, NULL);
4634
topIndex = 0;
4735
bottomIndex = 0;
4836
incompleteWork = 0;
@@ -53,7 +41,7 @@ ThreadPool::ThreadPool(int maxThreads)
5341

5442
void ThreadPool::initializeThreads()
5543
{
56-
for(int i = 0; i<maxThreads; ++i)
44+
for (unsigned int i = 0; i < _num_thread; ++i)
5745
{
5846
pthread_t tempThread;
5947
pthread_create(&tempThread, NULL, &ThreadPool::threadExecute, (void *) this );

threadpool.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ ThreadPool class manages all the ThreadPool related activities. This includes ke
4343
*/
4444
class ThreadPool{
4545
public:
46-
ThreadPool();
47-
ThreadPool(int maxThreadsTemp);
46+
explicit ThreadPool(unsigned int num_thread);
4847
virtual ~ThreadPool();
4948

5049
void destroyPool(int maxPollSecs);
@@ -61,7 +60,7 @@ class ThreadPool{
6160

6261

6362
private:
64-
int maxThreads;
63+
unsigned int _num_thread;
6564

6665
pthread_cond_t condCrit;
6766
sem_t availableWork;

0 commit comments

Comments
 (0)