-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThreadPool.c
80 lines (73 loc) · 2.48 KB
/
ThreadPool.c
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
#include "ThreadPool.h"
#include <stdlib.h>
#include <pthread.h>
#include "c_data_structures/Queue.h"
#include <stdbool.h>
#include <stdio.h>
void *thread_job(void *arg);
void add_task(ThreadPool *thread_pool, Task *task);
ThreadPool *thread_pool_constructor(int num_threads)
{
ThreadPool *thread_pool = (ThreadPool *)malloc(sizeof(ThreadPool));
thread_pool->num_threads = num_threads;
thread_pool->add_task = add_task;
thread_pool->active = 1;
thread_pool->pool = (pthread_t *)malloc(sizeof(pthread_t[num_threads]));
thread_pool->task_queue = queue_constructor(false, 0);
thread_pool->mutex = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER;
thread_pool->cond = (pthread_cond_t)PTHREAD_COND_INITIALIZER;
pthread_mutex_lock(&thread_pool->mutex);
for (int i = 0; i < num_threads; i++)
{
pthread_create(&thread_pool->pool[i], NULL, thread_job, thread_pool);
}
pthread_mutex_unlock(&thread_pool->mutex);
return thread_pool;
}
void thread_pool_destructor(ThreadPool *thread_pool)
{
thread_pool->active = 0;
pthread_cond_broadcast(&thread_pool->cond);
for (int i = 0; i < thread_pool->num_threads; i++)
{
pthread_join(thread_pool->pool[i], NULL);
}
free(thread_pool->pool);
queue_destructor(&thread_pool->task_queue);
pthread_mutex_destroy(&thread_pool->mutex);
pthread_cond_destroy(&thread_pool->cond);
}
Task *task_constructor(void *(*task_fn)(void *arg), void *args)
{
Task *task = (Task *)malloc(sizeof(Task));
task->task_fn = task_fn;
task->args = args;
return task;
}
void *thread_job(void *arg)
{
ThreadPool *thread_pool = (ThreadPool *)arg;
while (thread_pool->active)
{
pthread_mutex_lock(&thread_pool->mutex);
pthread_cond_wait(&thread_pool->cond, &thread_pool->mutex);
Task *task = (Task *)thread_pool->task_queue.peek(&thread_pool->task_queue);
Task task_temp;
task_temp.task_fn = task->task_fn;
task_temp.args = task->args;
thread_pool->task_queue.dequeue(&thread_pool->task_queue);
pthread_mutex_unlock(&thread_pool->mutex);
if (task_temp.task_fn != NULL)
{
task_temp.task_fn(task_temp.args);
}
}
return NULL;
}
void add_task(ThreadPool *thread_pool, Task *task)
{
pthread_mutex_lock(&thread_pool->mutex);
thread_pool->task_queue.enqueue(task, sizeof(*task), &thread_pool->task_queue);
pthread_cond_signal(&thread_pool->cond);
pthread_mutex_unlock(&thread_pool->mutex);
}