-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththread_pool.h
66 lines (50 loc) · 1.5 KB
/
thread_pool.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
#ifndef THREAD_POOL_H
#define THREAD_POOL_H
#include <pthread.h>
#include <stdlib.h>
class thread_pool {
public:
class task {
public:
virtual ~task() {}
virtual void run() = 0;
};
private:
class task_queue {
private:
struct task_queue_node {
class task* task;
task_queue_node *next;
task_queue_node(class task* task) : task(task), next(nullptr) {}
};
task_queue_node *head;
task_queue_node *tail;
// Task queue synchronization;
pthread_mutex_t queue_rwlock;
pthread_cond_t queue_available;
// Empty queue synchronization;
pthread_cond_t queue_empty;
public:
task_queue();
~task_queue();
int n_tasks;
void lock();
void unlock();
void wait();
void broadcast();
void broadcast_queue_empty();
void insert(task* task);
task* next();
} task_queue;
pthread_t *threads;
volatile int running;
volatile int n_threads;
volatile int active;
static void* thread_run(void *t_pool);
public:
thread_pool(int n_workers);
~thread_pool();
void add_task(task* task);
void wait_all();
};
#endif