-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreadpool.c
More file actions
168 lines (144 loc) · 3.83 KB
/
threadpool.c
File metadata and controls
168 lines (144 loc) · 3.83 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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <stdbool.h>
#define UNUSED(x) (void)(x)
#define THREADS_NUMBER 4
typedef struct {
pthread_t threads [THREADS_NUMBER];
pthread_mutex_t mutex;
pthread_cond_t cond_var;
bool ret;
}threadPool;
threadPool pool = (threadPool){.mutex = PTHREAD_MUTEX_INITIALIZER, .cond_var = PTHREAD_COND_INITIALIZER, .ret = false};
struct task{
void* (*func_ptr)(void*);
void *args;
struct task *next;
};
typedef struct task task;
typedef struct {
task *front;
task *rear;
size_t size;
}tasksQue;
tasksQue taskQueue = (tasksQue) {.front = NULL, .rear = NULL, .size = 0};
int EnqueueTask(void* (*func)(void*), void *args){
task *t = (task*)malloc(sizeof(task));
if (t == NULL){
fprintf(stderr,"mallocing a task failed...\n");
return EXIT_FAILURE;
}
t->func_ptr = func;
t->next = NULL;
t->args = args;
if (taskQueue.rear != NULL){
taskQueue.rear->next = t;
}
taskQueue.rear = t;
if (taskQueue.front == NULL){
taskQueue.front = t;
}
taskQueue.size++;
pthread_cond_signal(&pool.cond_var);
return EXIT_SUCCESS;
}
task* DequeueTask(void){
task *t = taskQueue.front;
taskQueue.front = taskQueue.front->next;
if (taskQueue.front == NULL){
taskQueue.rear = NULL;
}
taskQueue.size--;
return t;
}
bool isEmpty(void){
return (taskQueue.size == 0);
}
void *threadWait(void *args){
UNUSED(args);
while (true) {
pthread_mutex_lock(&pool.mutex);
while (isEmpty() && !pool.ret){
pthread_cond_wait(&pool.cond_var,&pool.mutex);
}
if (pool.ret){
pthread_mutex_unlock(&pool.mutex);
return NULL;
}
task *t = DequeueTask();
void* (*func)(void*) = t->func_ptr;
pthread_mutex_unlock(&pool.mutex);
(*func)(t->args);
free(t);
}
return NULL;
}
int threads_init(void){
int err = pthread_mutex_init(&pool.mutex,NULL);
if (err != 0){
fprintf(stderr,"failed to initialize mutex, quitting ...\n");
return EXIT_FAILURE;
}
err = pthread_cond_init(&pool.cond_var,NULL);
if (err != 0){
fprintf(stderr,"failed to initialize the condition variable, quitting ...\n");
return EXIT_FAILURE;
}
for (int i = 0; i < THREADS_NUMBER; i++){
if (pthread_create(&pool.threads[i],NULL,threadWait,NULL) != 0){
fprintf(stderr,"failed to create thread %d, quitting ...\n",i+1);
return EXIT_FAILURE;
}
}
return EXIT_SUCCESS;
}
void threads_join(void){
pthread_mutex_lock(&pool.mutex);
pool.ret = true;
pthread_mutex_unlock(&pool.mutex);
pthread_cond_broadcast(&pool.cond_var);
for (int i = 0; i < THREADS_NUMBER; i++){
if (pthread_join(pool.threads[i],NULL) != 0){
fprintf(stderr,"failed to join thread %d\n",i+1);
}
}
}
void threads_cleanup(void){
pthread_mutex_destroy(&pool.mutex);
pthread_cond_destroy(&pool.cond_var);
}
//testing function
void* print_hello(void *arg){
UNUSED(arg);
for(int i = 0; i < 5; i++){
printf("hello %d\n",i);
}
return NULL;
}
//testing function
void* print_string(void *arg){
char *s = (char*) arg;
printf("%s\n",s);
return NULL;
}
int main (void){
int err = threads_init();
if (err == EXIT_FAILURE){
return EXIT_FAILURE;
}
// add tasks
EnqueueTask(&print_hello,NULL);
EnqueueTask(&print_string,"wazzaz");
EnqueueTask(&print_string,"wazzaz");
EnqueueTask(&print_string,"wazzaz");
EnqueueTask(&print_string,"wazzaz");
EnqueueTask(&print_string,"wazzaz");
//allow some time for tasks to finish
sleep(3);
// finish the program
threads_join();
threads_cleanup();
return EXIT_SUCCESS;
}