-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy paththread_pool.h
More file actions
72 lines (55 loc) · 1.64 KB
/
Copy paththread_pool.h
File metadata and controls
72 lines (55 loc) · 1.64 KB
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
#ifndef __THREAD_POOL_H
#define __THREAD_POOL_H
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <pthread.h>
#include <semaphore.h>
#include "tsqueue.h"
#ifndef BOOL
#define BOOL int
#endif
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
#define BUSY_THRESHOLD 0.5 //(busy thread)/(all thread threshold)
#define MANAGE_INTERVAL 20 //tp manage thread sleep interval, every MANAGE_INTERVAL seconds, manager thread will try to recover idle threads as BUSY_THRESHOLD
#ifdef __cplusplus
extern "C" {
#endif
typedef struct tp_thread_info_s TpThreadInfo;
typedef struct tp_thread_pool_s TpThreadPool;
typedef void (*process_job)(void *arg);
//thread info
struct tp_thread_info_s {
pthread_t thread_id; //thread id num
BOOL stop_flag; //whether stop the thread
sem_t *event_sem;
process_job proc_fun;
void *arg;
TpThreadPool *tp_pool;
};
//main thread pool struct
struct tp_thread_pool_s {
unsigned min_th_num; //min thread number in the pool
unsigned max_th_num; //max thread number in the pool
TSQueue *busy_q; //busy queue
TSQueue *idle_q; //idle queue
TpThreadInfo *manage;
float busy_threshold; //
unsigned manage_interval; //
};
TpThreadPool *tp_create(unsigned min_num, unsigned max_num);
void tp_close(TpThreadPool *pTp, BOOL wait);
int tp_process_job(TpThreadPool *pTp, process_job proc_fun, void *arg);
float tp_get_busy_threshold(TpThreadPool *pTp);
int tp_set_busy_threshold(TpThreadPool *pTp, float bt);
unsigned tp_get_manage_interval(TpThreadPool *pTp);
int tp_set_manage_interval(TpThreadPool *pTp, unsigned mi); //mi - manager interval time, in second
#ifdef __cplusplus
}
#endif
#endif