-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreadpool.h
More file actions
95 lines (78 loc) · 2.53 KB
/
threadpool.h
File metadata and controls
95 lines (78 loc) · 2.53 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
#include <pthread.h>
/**
* threadpool.h
*
* This file declares the functionality associated with
* your implementation of a threadpool.
*/
// maximum number of threads allowed in a pool
#define MAXT_IN_POOL 200
/**
* the pool holds a queue of this structure
*/
typedef struct work_st{
int (*routine) (void*); //the threads process function
void * arg; //argument to the function
struct work_st* next;
} work_t;
/**
* The actual pool
*/
typedef struct _threadpool_st {
int num_threads; //number of active threads
int qsize; //number in the queue
pthread_t *threads; //pointer to threads
work_t* qhead; //queue head pointer
work_t* qtail; //queue tail pointer
pthread_mutex_t qlock; //lock on the queue list
pthread_cond_t q_not_empty; //non empty and empty condidtion vairiables
pthread_cond_t q_empty;
int shutdown; //1 if the pool is in distruction process
int dont_accept; //1 if destroy function has begun
} threadpool;
// "dispatch_fn" declares a typed function pointer. A
// variable of type "dispatch_fn" points to a function
// with the following signature:
//
// int dispatch_function(void *arg);
typedef int (*dispatch_fn)(void *);
/**
* create_threadpool creates a fixed-sized thread
* pool. If the function succeeds, it returns a (non-NULL)
* "threadpool", else it returns NULL.
* this function should:
* 1. input sanity check
* 2. initialize the threadpool structure
* 3. initialized mutex and conditional variables
* 4. create the threads, the thread init function is do_work and its argument is the initialized threadpool.
*/
threadpool* create_threadpool(int num_threads_in_pool);
/**
* dispatch enter a "job" of type work_t into the queue.
* when an available thread takes a job from the queue, it will
* call the function "dispatch_to_here" with argument "arg".
* this function should:
* 1. create and init work_t element
* 2. lock the mutex
* 3. add the work_t element to the queue
* 4. unlock mutex
*
*/
void dispatch(threadpool* from_me, dispatch_fn dispatch_to_here, void *arg);
/**
* The work function of the thread
* this function should:
* 1. lock mutex
* 2. if the queue is empty, wait
* 3. take the first element from the queue (work_t)
* 4. unlock mutex
* 5. call the thread routine
*
*/
void* do_work(void* p);
/**
* destroy_threadpool kills the threadpool, causing
* all threads in it to commit suicide, and then
* frees all the memory associated with the threadpool.
*/
void destroy_threadpool(threadpool* destroyme);