forked from jessek/hashdeep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththreadpool.h
78 lines (73 loc) · 2.08 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
78
/****************************************************************
*** THREADING SUPPORT
****************************************************************/
#ifndef THREADPOOL_H
#define THREADPOOL_H
#include <stdio.h>
#include <pthread.h>
#include <algorithm>
#include <queue>
#include <vector>
class mutex_t {
public:
mutable pthread_mutex_t mutex;
mutex_t(){
if(pthread_mutex_init(&mutex,NULL)){
perror("pthread_mutex_init failed");
exit(1);
}
}
~mutex_t(){
/*
* note that we do not destroy the mutex.
* On windows this seems to cause problems, so we just don't bother.
*
* if(pthread_mutex_destroy(&mutex)){
* perror("pthread_mutex_destroy failed");
* exit(1);
* }
*/
}
void lock() const{
if(pthread_mutex_lock(&mutex)){
perror("pthread_mutex_lock failed");
exit(1);
}
}
void unlock() const{
if(pthread_mutex_unlock(&mutex)){
perror("pthread_mutex_unlock failed");
exit(1);
}
}
};
class threadpool: public std::vector<class worker *> {
public:
static void win32_init(); // must be called under win32 to initialize posix threads
mutex_t M; // protects the following variables
volatile unsigned int numworkers;
volatile unsigned int freethreads;
pthread_cond_t TOMAIN;
pthread_cond_t TOWORKER;
std::queue<class file_data_hasher_t *> work_queue;
unsigned int get_free_count();
void schedule_work(class file_data_hasher_t *);
bool all_free() ;
void wait_till_all_free();
void kill_all_workers();
static int numCPU();
threadpool(int numworkers);
~threadpool();
unsigned int num_workers();
};
class worker {
public:
static void * start_worker(void *arg){return ((worker *)arg)->run();};
worker(class threadpool *master_,int workerid_): master(master_),workerid(workerid_){}
class threadpool *master; // my master
pthread_t thread; // my thread; set when I am created
int workerid; // my workerID, numbered 0 through numworkers-1
void *run();
void do_work(class file_data_hasher_t *); // must delete fdht when done
};
#endif