-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreadpool.h
More file actions
84 lines (73 loc) · 2.35 KB
/
threadpool.h
File metadata and controls
84 lines (73 loc) · 2.35 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
// You can modify this file however you like.
#ifndef THREADPOOL_H
#define THREADPOOL_H
#include <pthread.h>
#include <stdbool.h>
typedef void (*thread_func_t)(void* arg);
typedef struct ThreadPool_job_t {
thread_func_t func; // function pointer
void* arg; // arguments for that function
struct ThreadPool_job_t* next; // pointer to the next job in the queue
int length;
} ThreadPool_job_t;
typedef struct {
unsigned int size; // no. jobs in the queue
ThreadPool_job_t* head; // pointer to the first (shortest) job
ThreadPool_job_t *tail;
} ThreadPool_job_queue_t;
typedef struct {
int num_threads;
int active;
pthread_t *threads; // pointer to the array of thread handles
ThreadPool_job_queue_t jobs; // queue of jobs waiting for a thread to run
pthread_mutex_t lock;
pthread_cond_t signal;
int idlecount;
} ThreadPool_t;
/**
* C style constructor for creating a new ThreadPool object
* Parameters:
* num - Number of threads to create
* Return:
* ThreadPool_t* - Pointer to the newly created ThreadPool object
*/
ThreadPool_t* ThreadPool_create(unsigned int num);
/**
* C style destructor to destroy a ThreadPool object
* Parameters:
* tp - Pointer to the ThreadPool object to be destroyed
*/
void ThreadPool_destroy(ThreadPool_t* tp);
/**
* Add a job to the ThreadPool's job queue
* Parameters:
* tp - Pointer to the ThreadPool object
* func - Pointer to the function that will be called by the serving thread
* arg - Arguments for that function
* Return:
* true - On success
* false - Otherwise
*/
bool ThreadPool_add_job(ThreadPool_t* tp, thread_func_t func, void* arg, int job_length);
/**
* Get a job from the job queue of the ThreadPool object
* Parameters:
* tp - Pointer to the ThreadPool object
* Return:
* ThreadPool_job_t* - Next job to run
*/
ThreadPool_job_t* ThreadPool_get_job(ThreadPool_t* tp);
/**
* Start routine of each thread in the ThreadPool Object
* In a loop, check the job queue, get a job (if any) and run it
* Parameters:
* tp - Pointer to the ThreadPool object containing this thread
*/
void* Thread_run(void* arg);
/**
* Ensure that all threads are idle and the job queue is empty before returning
* Parameters:
* tp - Pointer to the ThreadPool object
*/
void ThreadPool_check(ThreadPool_t *tp);
#endif