-
Notifications
You must be signed in to change notification settings - Fork 0
/
thread_pool.h
93 lines (76 loc) · 1.77 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
/*
* Thread pool pattern implementation in C
* No references this time
*
* Author: Rytis Karpuška
*/
#ifndef __THREAD_POOL_H
#define __THREAD_POOL_H
#include <pthread.h>
#include "mpmc_lf_queue.h"
struct thread_pool {
struct mpmcq *wq;
int num_threads;
volatile int num_waiting_threads;
volatile int num_enqueued_tasks;
volatile int stop;
volatile int pause;
pthread_cond_t cond;
pthread_mutex_t mutex;
pthread_t thread[];
};
/*
* Create a new thread pool
*
* Arguments:
* num_threads - number of threads in a pool to create
*
* Returns:
* 0 - on success
* negative error code - on failure
*/
struct thread_pool *tp_create(unsigned int num_threads);
/*
* Stop thread pool and release its resources
*
* Note: this is not thread safe, thus caller must take care,
* that no new enqueues happens while this function is in affect
*
* Arguments:
* tp - thread pool previsouly returned by tp_create
*
* Return:
* 0 - on success
*/
int tp_destroy(struct thread_pool *tp);
/*
* Enqueue a task for execution.
*
* Arguments:
* tp - pointer to struct thread_pool previously returned by tp_create
* task - pointer to function of work
* arg - pointer to arguments passed to that function
*
* Returns:
* 0 - on success
* negative error code - on failure
*/
int tp_enqueueTask(struct thread_pool *tp, void (*task)(void *), void *arg);
/*
* Pause threads execution.
*
* Arguments:
* tp - struct thread pool previously returned by tp_create
*
*/
void tp_pause(struct thread_pool *tp);
/*
* Resume threads execution if it was previusly pused
*
* Arguments:
* tp - struct thread pool previously returned by tp_create
*
*/
void tp_resume(struct thread_pool *tp);
#endif