CSyncQueue method Pop() will automatically block and wait while the queue has no elements. It will wait for a given timeout until some other thread push element to the queue. CSyncQueue method Push() will automatically block and wait while the queue is full and contains maximum allowed amount of elements in it. It will wait for a given timeout until some other thread pops one or more elements from the queue (or maybe erases them via iterators).
CSyncQueue is designed to be used primarily for producer-consumer queues, but additionally it supports the iterator-based access. So, for example, it is possible to remove an arbitrary element from a queue using method Erase(it) in CSyncQueue::TAccessGuard.
Typical use of CSyncQueue in producer-consumer environment can be as follows:
typedef CSyncQueue<TSomeObject> TObjQueue;
TObjQueue s_queue(kSomeMaxSize);
class CProducer : public CThread { protected: virtual void* Main(void) { while (1) { s_queue.Push(Produce()); } } private: TSomeObject Produce() { ... } };
class CConsumer : public CThread { protected: virtual void* Main(void) { while (1) { Consume(s_queue.Pop()); } } private: void Consume(TSomeObject obj) { ... } };
It is safe to use CSyncQueue object with multiple producers and multiple consumers.
Extended use of CSyncQueue class for iterator-based access or for performing some bulk operations may look like following:
void FunctionWithGuard(void) { TObjQueue::TAccessGuard guard(s_queue);
for (TObjQueue::TAccessGuard::TIterator it = guard.Begin(); it != guard.End(); ) { if (NeedErase(*it)) { it = guard.Erase(it); } else { ++it; } }
while (HasMoreSomeObjects()) { guard.Queue().Push(GetNextSomeObject()); } }
CSyncQueue::TAccessGuard object here ensures that while the function is working other threads will not be able to push or pop any elements from the queue. Methods Push() and Pop() in other threads will block and wait while some CSyncQueue::TAccessGuard object is active.
1.4.6
Modified on Wed Dec 09 08:20:18 2009 by modify_doxy.py rev. 173732