-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreadPool.h
More file actions
67 lines (47 loc) · 1.4 KB
/
threadPool.h
File metadata and controls
67 lines (47 loc) · 1.4 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
// Omri Fridental
#include "osqueue.h"
#include <pthread.h>
#ifndef __THREAD_POOL__
#define __THREAD_POOL__
typedef enum destroy_type {
DESTROY_IMMIDETLY = 1,
DESTROY_WHEN_QUEUE_EMPTY = 2
} destroy_t;
/**
* @struct threadpool
* @brief The threadpool struct
*
* @var tasksQueue queue of tasks functions to run.
* @var N number of thread running.
* @var threads array of threads.
* @var destroying a flag to notify if the threadpool is currently destroying.
* @var queueLock a mutex on the queeu.
* @var conditional a conditional variable to notify working threads.
* @var tasksPendingCount a count of how many tasks waits in queue.
*/
typedef struct thread_pool
{
// a queue of tasks.
OSQueue* tasksQueue;
int taskPendingCount;
// an array of threads.
int N;
pthread_t* threads;
// locking insertion while destroying.
int destroying;
destroy_t destoryType;
// locks for each thread pool funcs.
pthread_mutex_t queueLock;
pthread_cond_t conditional;
int numThreadsRunning;
} ThreadPool;
// Task: a struct of func + arg. and isDone boolean.
typedef struct task_t
{
void (* func_ptr) (void*);
void* arg;
} Task;
ThreadPool* tpCreate(int numOfThreads);
void tpDestroy(ThreadPool* threadPool, int shouldWaitForTasks);
int tpInsertTask(ThreadPool* threadPool, void (*computeFunc) (void *), void* param);
#endif