forked from buptmiao/threadpool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtaskqueue.h
58 lines (43 loc) · 999 Bytes
/
taskqueue.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
#ifndef _TASK_QUEUE_H_
#define _TASK_QUEUE_H_
#include <queue>
#include <pthread.h>
#include "taskbase.h"
using namespace std;
const size_t MAX_TASK_NUMBER = 10000;
/* 任务队列类:使用stl中queue实现,支持向队列添加和从队列获取任务
*
*
*/
class TaskQueue {
public:
TaskQueue();
virtual ~TaskQueue();
bool add_task(TaskBase *task);
bool add_task(TaskBase &task);
void wake_up_worker();
void wake_up_producer();
TaskBase *get_task();
bool wake_up_all_worker(){
if(0 != pthread_cond_broadcast(&worker_cond))
return false;
return true;
}
size_t size();
bool get_lock_flag() const{
return lock_flag;
}
private:
TaskQueue(const TaskQueue&);
TaskQueue& operator=(const TaskQueue&);
queue<TaskBase *> m_task_queue;
pthread_cond_t worker_cond;
pthread_cond_t producer_cond;
pthread_mutex_t queue_lock;
size_t queue_size;
bool lock_flag;
bool initial_locks();
bool destroy_locks();
bool do_add_task(TaskBase *);
};
#endif