2 #include <boost/circular_buffer.hpp> 3 #include <boost/thread.hpp> 4 #include "util/mutex.h" 20 typedef boost::circular_buffer<T> container_type;
21 typedef typename container_type::size_type size_type;
22 typedef typename container_type::value_type value_type;
45 if(m_container.full())
47 return BufferOverFlow;
51 BufferStatus bufferStatus = m_container.empty() ? BufferFirst : BufferNormal;
52 m_container.push_back(item);
53 return m_container.full() ? BufferFull : bufferStatus;
65 if(m_container.full())
67 return BufferOverFlow;
71 BufferStatus bufferStatus = m_container.empty() ? BufferFirst : BufferNormal;
72 m_container.push_back(item);
73 *ppFrontItem = &(m_container.front());
74 return m_container.full() ? BufferFull : bufferStatus;
85 m_container.push_front(item);
96 if(m_container.capacity()<nCount)
102 BufferStatus bufferStatus = m_container.empty() ? BufferFirst : BufferNormal;
103 for (
int i=0; i<nCount;++i)
105 m_container.push_back(*(pItems+i));
107 return m_container.full() ? BufferFull : bufferStatus;
118 return m_container.empty() ? NULL : &(m_container.front());
129 if(m_container.empty())
135 item = m_container.front();
136 m_container.pop_front();
149 if(m_container.empty())
155 m_container.pop_front();
156 if(ppItem!=0 && !m_container.empty())
158 *ppItem = &(m_container.front());
164 size_type size()
const {
166 return m_container.size();
170 return m_container.empty();
174 return m_container.full();
177 container_type m_container;
187 template<
typename Data>
191 typedef boost::circular_buffer<Data> container_type;
192 typedef typename container_type::size_type size_type;
193 typedef typename container_type::value_type value_type;
195 mutable boost::mutex m_mutex;
196 boost::condition_variable m_condition_variable;
204 container_type m_container;
215 explicit concurrent_queue(size_type capacity) : m_container(capacity),m_use_event(
true) {}
223 boost::mutex::scoped_lock lock(m_mutex);
224 m_use_event = bUseEvent;
235 boost::mutex::scoped_lock lock(m_mutex);
236 BufferStatus bufferStatus = m_container.empty() ? BufferFirst : BufferNormal;
237 if(m_container.full())
239 bufferStatus = BufferOverFlow;
243 m_container.push_back(item);
244 bufferStatus = m_container.full() ? BufferFull : bufferStatus;
248 m_condition_variable.notify_one();
257 boost::mutex::scoped_lock lock(m_mutex);
258 m_container.push_back(data);
261 m_condition_variable.notify_one();
270 boost::mutex::scoped_lock lock(m_mutex);
271 m_container.push_front(data);
273 m_condition_variable.notify_one();
278 boost::mutex::scoped_lock lock(m_mutex);
279 return m_container.empty();
282 bool try_pop(value_type& popped_value)
284 boost::mutex::scoped_lock lock(m_mutex);
285 if(m_container.empty())
290 popped_value=m_container.front();
291 m_container.pop_front();
295 void wait_and_pop(value_type& popped_value)
297 boost::mutex::scoped_lock lock(m_mutex);
298 while(m_container.empty())
300 m_condition_variable.wait(lock);
302 popped_value=m_container.front();
303 m_container.pop_front();
BufferStatus try_push(const value_type &item)
try push to back of the queue.
Definition: ParaRingBuffer.h:42
bool m_use_event
whether to use event to inform consumer when new data items are added to the queue.
Definition: ParaRingBuffer.h:202
it implements a producer/consumer(s) queue design pattern.
Definition: ParaRingBuffer.h:188
void push_front(value_type &data)
add a data item to the front of the queue.
Definition: ParaRingBuffer.h:268
different physics engine has different winding order.
Definition: EventBinding.h:32
simple scoped lock function
Definition: mutex.h:12
BufferStatus try_push(value_type &item)
try push to back of the queue.
Definition: ParaRingBuffer.h:233
void push_front(const value_type &item)
push to the front of the queue.
Definition: ParaRingBuffer.h:82
bool try_next(value_type **ppItem)
try pop from the front of the queue and return the front object after the pop.
Definition: ParaRingBuffer.h:146
void push(value_type &data)
add a data item to the back of the queue.
Definition: ParaRingBuffer.h:255
BufferStatus try_push_get_front(const value_type &item, value_type **ppFrontItem)
same as try_push, except that it also returns pointer to the front object.
Definition: ParaRingBuffer.h:62
cross platform mutex
Definition: mutex.h:95
bool try_pop(value_type &item)
try pop from the front of the queue.
Definition: ParaRingBuffer.h:126
void SetUseEvent(bool bUseEvent)
whether to use event to inform consumer when new data items are added to the queue.
Definition: ParaRingBuffer.h:221
CParaRingBuffer is normally used in a producer-consumer mode when producer threads produce items and ...
Definition: ParaRingBuffer.h:17
BufferStatus try_push_array(const value_type *pItems, int nCount)
try push to back of the queue.
Definition: ParaRingBuffer.h:93
value_type * try_front()
Definition: ParaRingBuffer.h:115