-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.cpp
60 lines (50 loc) · 1.01 KB
/
stack.cpp
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
#include <iostream>
#include <thread>
#include <mutex>
#include <unistd.h>
#include "stack.h"
#include "action.h"
using namespace std;
using namespace DP;
DP::Stack::Stack(Plane *plane)
: _stop(false)
{
for (int i = 0; i < NB_THREAD; i++)
_thread[i] = thread(Action(*this, plane));
}
DP::Stack::~Stack()
{
{
unique_lock<mutex> lock(_stop_mutex);
_stop = true;
}
_condition.notify_all();
for (int i = 0; i < _thread.size(); i++)
_thread[i].join();
}
DPTask *DP::Stack::dequeueTask()
{
DPTask *retval;
cout << "Dequeue task" << endl;
retval = _tasks.front();
_tasks.pop_front();
return retval;
}
void DP::Stack::enqueueTask(DPTask *task)
{
cout << "Enqueue task" << endl;
{
unique_lock<mutex> lock(_mutex);
_tasks.push_back(task);
}
_condition.notify_one();
}
bool DP::Stack::queueEmpty() const
{
return _tasks.empty();
}
bool DP::Stack::stopAsked()
{
unique_lock<mutex> lock(_stop_mutex);
return _stop;
}