-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathunlock_queue.h
More file actions
60 lines (56 loc) · 1.01 KB
/
unlock_queue.h
File metadata and controls
60 lines (56 loc) · 1.01 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
#ifndef __UNLOCK_QUEUE_H__
#define __UNLOCK_QUEUE_H__
template<class QElmType>
struct unlock_queue_node
{
struct unlock_queue_node *next;
QElmType data;
};
template<class QElmType>
class unlock_queue
{
public:
unlock_queue() {}
~unlock_queue() {}
bool init()
{
m_front = m_rear = new unlock_queue_node<QElmType>;
if (!m_front)
return false;
m_front->next = 0;
return true;
}
void destroy()
{
while (m_front)
{
m_rear = m_front->next;
delete m_front;
m_front = m_rear;
}
}
bool push(QElmType e)
{
struct unlock_queue_node<QElmType> *p = new unlock_queue_node<QElmType>;
if (!p)
return false;
p->next = 0;
m_rear->next = p;
m_rear->data = e;
m_rear = p;
return true;
}
bool pop(QElmType *e)
{
if (m_front == m_rear)
return false;
struct unlock_queue_node<QElmType> *p = m_front;
*e = p->data;
m_front = p->next;
delete p;
return true;
}
private:
struct unlock_queue_node<QElmType> * volatile m_front, * volatile m_rear;
};
#endif //__UNLOCK_QUEUE_H__