-
Notifications
You must be signed in to change notification settings - Fork 0
/
thrpl.h
58 lines (47 loc) · 1.28 KB
/
thrpl.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
#ifndef _THREAD_POOL_H_
#define _THREAD_POOL_H_
#include <pthread.h>
#define MAX_NUMBER_OF_THREADS 20
#define MIN_NUMBER_OF_THREADS 4
#define MAX_NUMBER_OF_TASKS 256
// #define MIN_NUMBER_OF_WAIT_TASKS 4
#define DEFAULT_NUMBER_OF_THREAD 4
typedef void *(*function)(void *);
typedef struct {
function func;
void *argv;
} Task;
typedef struct {
int head;
int rear;
int count;
int size;
Task *tasks;
} TaskQueue;
TaskQueue *TaskQueue_new();
int TaskQueue_enqueue(TaskQueue *self, Task task);
int TaskQueue_dequeue(TaskQueue *self, Task *out_task);
typedef struct {
TaskQueue *task_queue;
pthread_mutex_t mutex;
pthread_mutex_t thr_counter; // Guarder for ThreadPool.busy_thr_num
pthread_cond_t queue_not_full;
pthread_cond_t queue_not_empty;
pthread_t admin_tid;
pthread_t *threads;
size_t busy_thr_num;
size_t live_thr_num;
size_t min_thr_num;
size_t max_thr_num;
size_t wait_exit_thr_num;
int shutdown;
int graceful_shutdown;
} ThreadPool;
ThreadPool *ThreadPool_new();
void *ThreadPool_thread(void *thp);
void *ThreadPool_admin_thread(void *thp);
int ThreadPool_add_task(ThreadPool *self, Task task);
int ThreadPool_destroy(ThreadPool *self);
int ThreadPool_gracefully_destroy(ThreadPool *self);
int ThreadPool_free(ThreadPool *self);
#endif // _THREAD_POOL_H_