-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskQueue.h
More file actions
executable file
·62 lines (58 loc) · 1.1 KB
/
TaskQueue.h
File metadata and controls
executable file
·62 lines (58 loc) · 1.1 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
#ifndef _TASK_QUEUE_
#define _TASK_QUEUE_
#include <queue>
#include <boost/thread.hpp>
#include <boost/noncopyable.hpp>
#include <boost/function.hpp>
#include <stdio.h>
//typedef boost::function<void(void)> TASK_FUNC;
template<typename TASK_FUNC>
class TaskQueue:boost::noncopyable
{
public:
TaskQueue()
{
m_bStop = false;
}
void PushTask(const TASK_FUNC & task_func)
{
boost::unique_lock<boost::mutex> lock(my_mutex);
m_TaskQueue.push(task_func);
cond.notify_one();
}
TASK_FUNC TakeTask()
{
boost::unique_lock<boost::mutex> lock(my_mutex);
if(m_TaskQueue.size()==0)
{
cond.wait(lock);
}
//printf("queue 1\n");
if(m_TaskQueue.size() == 0 || m_bStop == true)
{
//TASK_FUNC temp;
//return temp;
//printf("queue 2\n");
return NULL;
}
//printf("queue 3\n");
TASK_FUNC task(m_TaskQueue.front());
m_TaskQueue.pop();
return task;
}
void stop()
{
m_bStop = true;
cond.notify_all();
}
int GetSize()
{
return m_TaskQueue.size();
}
private:
std::queue<TASK_FUNC> m_TaskQueue;
boost::condition_variable_any cond;
boost::mutex my_mutex;
bool m_bStop;
};
#endif