-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThreadPool.h
78 lines (57 loc) · 1.5 KB
/
ThreadPool.h
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
/*************************************************************************
> File Name: threadpoll.h
> Author: Weidong, ZHANG
> Mail: [email protected]
> Created Time: Thu 04 Dec 2014 08:55:22 PM PST
************************************************************************/
#ifndef THREADPOOL_H
#define THREADPOOL_H
#include <pthread.h>
#include <unistd.h>
#include <semaphore.h>
#include <iostream>
#include <vector>
using namespace std;
/*
WorkerThread class
This class needs to be sobclassed by the user.
*/
class WorkerThread{
public:
int id;
unsigned virtual executeThis()
{
return 0;
}
WorkerThread(int id) : id(id) {}
virtual ~WorkerThread(){}
};
/*
ThreadPool class manages all the ThreadPool related activities.
This includes keeping track of idle threads and ynchronizations between all threads.
*/
class ThreadPool{
public:
ThreadPool();
ThreadPool(int maxThreadsTemp);
virtual ~ThreadPool();
void destroyPool(int maxPollSecs);
bool assignWork(WorkerThread *worker);
bool fetchWork(WorkerThread **worker);
void initializeThreads();
static void *threadExecute(void *param);
static pthread_mutex_t mutexSync;
static pthread_mutex_t mutexWorkCompletion;
private:
int maxThreads;
// pthread_cond_t condCrit;
sem_t availableWork;
sem_t availableThreads;
//WorkerThread ** workerQueue;
vector<WorkerThread *> workerQueue;
int topIndex;
int bottomIndex;
int incompleteWork;
int queueSize;
};
#endif