Repository navigation
Expand file tree
/
Copy pathEasy_ThreadPool.c
More file actions
198 lines (174 loc) · 6.55 KB
/
Copy pathEasy_ThreadPool.c
File metadata and controls
198 lines (174 loc) · 6.55 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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <fcntl.h>
#include <assert.h>
/*
Author: goudan
Name : Easy_ThreadPool
Time : Thu 14 Apr 2022 14:49:32 -4:00
*/
// task node
struct Task{
void* (*callback_function)(void* arg); // callback function
void* arg; // arg
struct Task* next; // next node
};
// thread pool
struct ThreadPool{
int thread_number; // The number of this TreadPool
int queue_max_num; // The Max number of Task Queue
struct Task* head; // The Link Queue of Task
struct Task* tail;
pthread_t* pthread_ids; // All id of threads (Array)
pthread_mutex_t mutex_lock; // The lock of Manage Thread
// the condition locks
pthread_cond_t queue_empty; // empty lock
pthread_cond_t queue_not_empty; // not empty lock
pthread_cond_t queue_not_full; // not full lock
// the status of poll
int queue_task_num; // The number of Task currently
int queue_close; // Whether the queue had been closeed
int pool_close; // Whether the ThreadPool had been closed
};
struct ThreadPool* ThreadPool_init(int thread_num, int max_task_queue_num);
void* ThreadPool_Function(void *arg);
int ThreadPool_Add_Task(struct ThreadPool* poll,void* (*callback_function)(void* arg),void* arg);
int ThreadPool_destroy(struct ThreadPool* poll);
// the thread real use function
void* work(void* arg){
char* p = (char*)arg;
printf("Thread real Task function:%s\n",p);
sleep(1);
}
// Test Process Start Here
int main(){
struct ThreadPoll* pool = ThreadPool_init(10, 15);
ThreadPool_Add_Task(pool, work, "Task 1");
ThreadPool_Add_Task(pool, work, "Task 2");
ThreadPool_Add_Task(pool, work, "Task 3");
ThreadPool_Add_Task(pool, work, "Task 4");
ThreadPool_Add_Task(pool, work, "Task 5");
ThreadPool_Add_Task(pool, work, "Task 6");
ThreadPool_Add_Task(pool, work, "Task 7");
ThreadPool_Add_Task(pool, work, "Task 8");
ThreadPool_Add_Task(pool, work, "Task 9");
ThreadPool_Add_Task(pool, work, "Task 10");
ThreadPool_Add_Task(pool, work, "Task 11");
sleep(10);
ThreadPool_destroy(pool);
return 0;
}
// Initial the ThreadPoll Function
struct ThreadPool* ThreadPool_init(int thread_num, int max_task_queue_num){
struct ThreadPool* pool = NULL;
pool = malloc(sizeof(struct ThreadPool));
if(pool == NULL) {
printf("Create poll failed: malloc return NULL\n");
return NULL;
}
// set initial value
pool->pthread_ids = malloc(sizeof(pthread_t)*thread_num);
pool->thread_number = thread_num;
pool->queue_max_num = max_task_queue_num;
pool->queue_task_num = 0; // Current Task num set 0
pool->head = pool->tail = NULL; // Task Queue initial status
// mutex lock
pthread_mutex_init(&(pool->mutex_lock), NULL);
// condition lock
pthread_cond_init(&(pool->queue_empty), NULL);
pthread_cond_init(&(pool->queue_not_empty), NULL);
pthread_cond_init(&(pool->queue_not_full), NULL);
// the status of poll initalized
pool->pool_close = pool->queue_close = 0;
// the thread id queue initalized
for(int i=0;i<pool->thread_number;i++){
// create every thread and transfor the poll class to the function
pthread_create(&(pool->pthread_ids[i]), NULL, ThreadPool_Function, (void*)pool);
}
return pool;
}
// ThreadPoll_Thread Callback Fucntion
void* ThreadPool_Function(void *arg){
// arg(poll) save to use check the status of pool
struct ThreadPool* pool = (struct ThreadPool*) arg;
struct Task* pTask = NULL;
// thread function
while(1){
// MutexLock lock
pthread_mutex_lock(&(pool->mutex_lock));
// check the status of pool
while((pool->queue_task_num == 0) && !pool->pool_close){
// while the queue is empty the thread wait
pthread_cond_wait(&(pool->queue_not_empty), &(pool->mutex_lock));
}
// The ThreadPoll closed , thread exit
if(pool->pool_close){
pthread_mutex_unlock(&(pool->mutex_lock));
pthread_exit(NULL);
}
// Process Task current leave queue
pool->queue_task_num--;
// Get Task Queue head
pTask = pool->head;
if(pool->queue_task_num == 0){
pool->head = pool->tail = NULL;
}else{
pool->head = pTask->next;
}
// When the size of queue of Task is zero post the queue_empty cond siganl lock
if(pool->queue_task_num == 0){
pthread_cond_signal(&(pool->queue_empty));
}
// When the size of queue of Task is less than queue_max_num post the queue_not_full cond siganl lock
if(pool->queue_task_num == pool->queue_max_num){
pthread_cond_broadcast(&(pool->queue_not_full));
}
pthread_mutex_unlock(&(pool->mutex_lock));
// real work -> to process Task callback function
(*(pTask->callback_function) )(pTask->arg);
free(pTask);
pTask=NULL;
}
}
// Add the Task to ThreadPool's Task Queue
int ThreadPool_Add_Task(struct ThreadPool* pool,void* (*callback_function)(void* arg),void* arg){
// Check args
assert(pool!=NULL);
assert(callback_function!=NULL);
assert(arg!=NULL);
// Check ThreadPool status
pthread_mutex_lock(&(pool->mutex_lock));
while((pool->queue_max_num == pool->queue_task_num)&&!(pool->pool_close||pool->queue_close)){
pthread_cond_wait(&(pool->queue_not_full), &(pool->mutex_lock));
}
if(pool->pool_close||pool->queue_close){
pthread_mutex_unlock(&(pool->mutex_lock));
return -1;
}
// Add Task to ThreadPool's Task Queue
struct Task* task = NULL;
task = malloc(sizeof(struct Task));
if(task == NULL){
pthread_mutex_unlock(&(pool->mutex_lock));
printf("Create Task failed: malloc failed");
return -1;
}
task->callback_function = callback_function;
task->arg = arg;
task->next = NULL;
if(pool->head==NULL){
pool->head = pool->tail = task;
pthread_cond_broadcast(&(pool->queue_not_empty)); //tell the mutelock the queue is not empty
}else{
pool->tail->next = task;
pool->tail = task;
}
pool->queue_task_num ++;
pthread_mutex_unlock(&(pool->mutex_lock));
return 0;
}
// Destory the ThreadPoll
int ThreadPool_destroy(struct ThreadPool* poll){
}