-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththreadpool.h
73 lines (51 loc) · 1.36 KB
/
threadpool.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
#ifndef __THREADPOOL_H__SDDSFGGAB9980
#define __THREADPOOL_H__SDDSFGGAB9980
#define IF_NULL(p, e) \
if (NULL == (p)) { \
return (e);\
}
enum
{
SUCCESS = 0,
MALLOC_FAIL = -1,
PTHREAD_CREATE_FAIL = -2,
PTHREAD_POOL_NULL = -3,
TASK_FUNCTION_NULL = -4
};
enum
{
THREAD_INIT = 0,
THREAD_WAITTING,
THREAD_RUNNING,
THREAD_DIE
};
typedef void (*Func)(void *arg);
typedef struct task_element_t TaskElement;
struct task_element_t
{
Func m_pFunc; /* task function */
void *m_pArg; /* task function argument */
TaskElement *next;
};
typedef struct pthreadpool_t PthreadPool;
struct pthreadpool_t
{
pthread_t *m_pth; /* pthread array */
pthread_mutex_t m_mutex; /* pthread mutex */
pthread_cond_t m_cond; /* pthread condition */
int *m_pStatus; /* pthread status */
TaskElement *m_pTaskHead; /* task queue list head*/
TaskElement *m_pTaskTail; /* task queue list tail */
int m_iTaskNum; /* task queue num */
int m_iPthreadNum; /* pthread num */
};
typedef struct
{
PthreadPool *m_pool;
int m_iThread;
} PthreadStatus;
PthreadPool* PthreadPoolInit(int pthreadNums);
int pthreadPoolDestroy(PthreadPool *pool);
int PthreadPoolAddTask(PthreadPool *pool, Func fun, void *arg);
void *doPthreadPool(void *arg);
#endif