-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.cpp
More file actions
55 lines (52 loc) · 1.59 KB
/
queue.cpp
File metadata and controls
55 lines (52 loc) · 1.59 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
#include "queue.h"
using namespace std::chrono_literals;
Queue::Queue(bool &running)
{
this->running = running;
this->operationPointId = -1;
for (int i = 0; i <= 5; i++)
{
this->queueIds[i] = -1;
}
}
void Queue::customer(int id)
{
int position = id > 5 ? 5 : id;
//int position = 5;
while (running)
{
std::unique_lock<std::mutex> l(m_mutex);
if (position >= 0)
{
this->queueCond[position].wait_for(l,22500ms, [this, position] { return this->queueIds[position] == -1; });
if (position == 5)
{
this->queueIds[position] = id;
position--;
l.unlock();
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
else
{
this->queueIds[position] = id;
this->queueIds[position + 1] = -1;
l.unlock();
queueCond[position + 1].notify_one();
position--;
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
}
else
{
this->operationPointCond.wait_for(l,22000ms, [this]() { return this->operationPointId == -1; });
this->operationPointId = id;
this->queueIds[0] = -1;
// Make This Thread sleep for 2 Second
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
this->operationPointId = -1;
l.unlock();
this->operationPointCond.notify_one();
position = 5;
}
}
}