You.i Engine
YiConcurrentQueue.h
Go to the documentation of this file.
1 // © You i Labs Inc. 2000-2017. All rights reserved.
2 #ifndef _YI_CONCURRENT_QUEUE_H_
3 #define _YI_CONCURRENT_QUEUE_H_
4 
5 #include <queue>
6 
7 #include "thread/YiAutoMutex.h"
8 #include "thread/YiMutex.h"
10 
21 template<typename YI_DATA>
23 {
24 public:
29  : m_bStopWaiting(false)
30  {
31  }
32 
37  : m_bStopWaiting(false)
38  {
39  CYIAutoMutex autoMutex(other.m_mutex);
40  m_queue = other.m_queue;
41  }
42 
47  {
48  CYIAutoMutex autoLhsMutex(m_mutex);
49  CYIAutoMutex autoRhsMutex(rhs.m_mutex);
50  m_queue = rhs.m_queue;
51  return *this;
52  }
53 
57  void Push(const YI_DATA &rData)
58  {
59  CYIAutoMutex autoMutex(m_mutex);
60 
61  m_queue.push(rData);
62 
63  m_AvailableCondition.WakeOne();
64  }
65 
69  void WaitEmpty()
70  {
71  CYIAutoMutex autoMutex(m_mutex);
72  while (!m_queue.empty()) // protection against 'spurious wake-ups'
73  {
74  m_EmptyCondition.Wait(m_mutex);
75  }
76  }
77 
81  bool Empty() const
82  {
83  CYIAutoMutex autoMutex(m_mutex);
84  return m_queue.empty();
85  }
86 
91  bool TryPop(YI_DATA & rValue)
92  {
93  bool bSuccessfulLock = false;
94 
95  if (m_mutex.TryLock())
96  {
97  if(!m_queue.empty())
98  {
99  rValue = m_queue.front();
100  m_queue.pop();
101 
102  bSuccessfulLock = true;
103 
104  if (m_queue.empty())
105  {
106  m_EmptyCondition.WakeAll();
107  }
108  }
109 
110  m_mutex.Unlock();
111  }
112 
113  return bSuccessfulLock;
114  }
115 
121  bool WaitAndPop(YI_DATA & rValue)
122  {
123  bool bRet = false;
124 
125  CYIAutoMutex autoMutex(m_mutex);
126 
127  while(m_queue.empty() && !m_bStopWaiting)
128  {
129  m_AvailableCondition.Wait(m_mutex);
130  }
131 
132  m_bStopWaiting = false;
133 
134  if (!m_queue.empty())
135  {
136  rValue = m_queue.front();
137  m_queue.pop();
138 
139  if (m_queue.empty())
140  {
141  m_EmptyCondition.WakeAll();
142  }
143 
144  bRet = true;
145  }
146 
147  return bRet;
148  }
149 
155  void StopWaiting()
156  {
157  CYIAutoMutex autoMutex(m_mutex);
158 
159  m_bStopWaiting = true;
160  m_AvailableCondition.WakeOne();
161  }
162 
163 private:
164  std::queue<YI_DATA> m_queue;
165  mutable CYIMutex m_mutex;
166 
167  bool m_bStopWaiting;
168  CYIWaitCondition m_EmptyCondition;
169  CYIWaitCondition m_AvailableCondition;
170 
171 };
172 
175 #endif // _YI_CONCURRENT_QUEUE_H_
Definition: YiMutex.h:110
bool Unlock()
bool Empty() const
Definition: YiConcurrentQueue.h:81
void StopWaiting()
Definition: YiConcurrentQueue.h:155
void WaitEmpty()
Definition: YiConcurrentQueue.h:69
void Push(const YI_DATA &rData)
Definition: YiConcurrentQueue.h:57
CYIConcurrentQueue()
Definition: YiConcurrentQueue.h:28
A simple, thread safe queue.
Definition: YiConcurrentQueue.h:22
bool TryPop(YI_DATA &rValue)
Definition: YiConcurrentQueue.h:91
bool WaitAndPop(YI_DATA &rValue)
Definition: YiConcurrentQueue.h:121
bool TryLock()
bool Wait(CYIMutex &rMutex)
A class used to block a thread until a condition is met, as signaled by a different thread...
Definition: YiWaitCondition.h:64
Definition: YiAutoMutex.h:23
CYIConcurrentQueue & operator=(const CYIConcurrentQueue< YI_DATA > &rhs)
Definition: YiConcurrentQueue.h:46
CYIConcurrentQueue(const CYIConcurrentQueue< YI_DATA > &other)
Definition: YiConcurrentQueue.h:36