-
Notifications
You must be signed in to change notification settings - Fork 9
/
thread_pool.h
executable file
·101 lines (67 loc) · 1.93 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#ifndef __THREAD_POOL__H_
#define __THREAD_POOL__H_
#include "syn_primitive.h"
#include "double_list.h"
#include "seq_queue.h"
typedef enum {EThread_pool_unknown, EThread_pool_alloc, EThread_pool_init
, EThread_pool_run, EThread_pool_exit, EThread_pool_MAX}EThread_pool_status;
#define RELEASE_THREAD_INTERVAL 5*60
//线程函数原型
#if (WIN32)
typedef DWORD ( *THREAD_FUNC)(void *);
#else
typedef void (*THREAD_FUNC)(void *);
#endif
typedef void (*USER_FUNC)(void *thread_para);
typedef struct {
USER_FUNC timeout_callback;
unsigned long time_out;
}time_out_t;
typedef struct
{
USER_FUNC process_job;
USER_FUNC release_job;
void *args;
time_out_t time_out_info;
}thread_job_t;
typedef struct
{
thread_job_t thread_para;
unsigned int pri;
BOOL busy;
BOOL release;
unsigned long launch_time;
unsigned long time_out;
EThread_pool_status *pool_status;
thread_handle h_thread;
//thread_id id;
condition_handle thread_cond;
mutex_handle thread_lock;
}thread_info_t;
typedef struct
{
unsigned int pri;
unsigned int fix_thread_num;
unsigned int max_thread_num;
unsigned int pool_thread_num;
condition_handle manage_cond;
mutex_handle mange_lock;
unsigned long release_threads_interval;
d_list_t *idle_threads;
d_list_t *busy_threads;
seq_queue_t *task_queue;
sem_handle sem_inc;
thread_handle h_id;
//thread_id id;
EThread_pool_status status;
}thread_pool_t;
//创建线程池
thread_pool_t *createThreadPool(unsigned int fix_thread_num, unsigned int max_thread_num);
//销毁线程池
void destroyThreadPool(thread_pool_t *pool);
//添加任务到线程池
BOOL addJobToThreadPool(thread_pool_t *pool, USER_FUNC process_job, void *args);
BOOL addJobToThreadPoolEx(thread_pool_t *pool, USER_FUNC process_job
, USER_FUNC release_job, void *args, time_out_t *time_out);
void tp_sleep(unsigned int ms);
#endif //__THREAD_POOL__H_