|
Core Library
1.7.0.0
Library containing core utilities and tools for threading, networking, logging, INI and CSV file management etc.
|
Class defining a concurrent queue. More...
#include <ConcurrentQueue.h>
Public Types | |
| using | container_type = std::deque< T > |
| Typedef for container type. | |
Public Member Functions | |
| ConcurrentQueue ()=default | |
| Default constructor. | |
| ConcurrentQueue (const ConcurrentQueue &)=delete | |
| Copy constructor deleted. | |
| ConcurrentQueue & | operator= (const ConcurrentQueue &)=delete |
| Copy assignment operator deleted. | |
| ConcurrentQueue (ConcurrentQueue &&)=delete | |
| Move constructor deleted. | |
| ConcurrentQueue & | operator= (ConcurrentQueue &&)=delete |
| Move assignment operator deleted. | |
| ~ConcurrentQueue ()=default | |
| Destructor. More... | |
| size_t | Size () const |
| Size of the queue. More... | |
| bool | Empty () const |
| Is the queue empty. More... | |
| void | Push (T &&item) |
| Push an item onto the queue. More... | |
| void | Push (const T &item) |
| Push an item onto the queue. More... | |
| void | BreakPopWait () |
| Break out of waiting on a Pop method. More... | |
| bool | Pop (T &item) |
| Pop an item off the queue if there are any else wait. More... | |
| void | PopThrow (T &item) |
| Pop an item off the queue if there are any else wait. More... | |
| bool | TryPop (T &item) |
| Pop an item off the queue if there are any else return. More... | |
| void | TryPopThrow (T &item) |
| Pop an item off the queue. More... | |
| bool | TimedPop (unsigned int timeoutMilliseconds, T &item) |
| Pop an item off the queue but only wait for a given amount of time. More... | |
| void | TimedPopThrow (unsigned int timeoutMilliseconds, T &item) |
| Pop an item off the queue but only wait for a given amount of time. More... | |
| bool | TrySteal (T &item) |
| Steal an item from the back of the queue if there are any else return. More... | |
| void | TryStealThrow (T &item) |
| Steal an item from the back of the queue. More... | |
| const T * | Peek (size_t index) const |
| Take a peek at an item at a given index on the queue. More... | |
| void | Clear () |
| Clear the queue. More... | |
| template<typename F > | |
| void | Clear (F deleter) |
| Clear the queue. More... | |
| container_type | TakeAll () |
| Take all items from the queue and return them, thus clearing down the internal queue. More... | |
Private Types | |
| enum | eQueueEnd { eQueueEnd::front, eQueueEnd::back } |
| Enumeration controlling end of queue to pop from. More... | |
Private Member Functions | |
| bool | PopNow (T &item, eQueueEnd whichEnd=eQueueEnd::front) |
| Pop an item off the queue. More... | |
| void | PopFront (T &item) |
| Pop an item off the front of the queue. More... | |
| void | PopBack (T &item) |
| Pop an item off the back of the queue. More... | |
Private Attributes | |
| std::mutex | m_mutex |
| Synchronization mutex. | |
| SyncEvent | m_itemEvent |
| Synchronization event. More... | |
| container_type | m_queue {} |
| Underlying deque container acting as the queue. | |
Class defining a concurrent queue.
This class implements a fully thread-safe queue that can be used with single/multiple producer thread(s) and single/multiple consumer thread(s).
It is up to the caller to make sure individual queue items get deallocated correctly. Preferably by using RAII objects as queue items or wrapping underlying data in std::shared_ptr (using a custom deallocator if necessary - see example custom deleters: SingleItemDeleter and ArrayDeleter).
The template T must be a copyable and movable type.
|
strongprivate |
|
default |
Destructor.
If the queue items aren't RAII-like objects and so don't manager their own memory then make sure you call the second Clear method, defined later, that takes a deleter functor to tidy up the memory of each queue item.
|
inline |
Break out of waiting on a Pop method.
Useful to force consumer(s) to break out of wait on Pop etc.
|
inline |
Clear the queue.
If this method is called while a thread is blocking on the item event then due to interleving of calls and mutex lock timings this may cause the list to be cleared before a waiting pop function has had a chance to pop anything from the queue. This is safe but what it means is the pop function will not find anything to pop and will either throw or return false to indicate that nothing was popped off the queue.
|
inline |
Clear the queue.
| [in] | deleter | - If queue items are not RAII objects that will tidy up after themselves then you can pass in a suitable deleter to delete each queue item. |
It is also expected that the queue items are raw pointers.
If this method is called while a thread is blocking on the item event then due to interleving of calls and mutex lock timings this may cause the list to be cleared before a waiting pop function has had a chance to pop anything from the queue. This is safe but what it means is the pop function will not find anything to pop and will either throw or return false to indicate that nothing was popped off the queue.
|
inline |
Is the queue empty.
|
inline |
Take a peek at an item at a given index on the queue.
| [in] | index | - Zero-based index of the queue item to peek at. |
This method returns a nullptr if the index does not exist in the queue. The use of this method can be dangerous if there are multiple consumers. or if there is a single consumer but the method is called from a different thread to the consumer.
|
inline |
Pop an item off the queue if there are any else wait.
| [out] | item | - The popped item, only valid if returns true. |
Method will block forever or until an item is placed on the queue.
|
inlineprivate |
Pop an item off the back of the queue.
| [out] | item | - Item popped off back. |
|
inlineprivate |
Pop an item off the front of the queue.
| [out] | item | - Item popped off front. |
|
inlineprivate |
Pop an item off the queue.
| [out] | item | - Item popped off queue. |
| [out] | whichEnd | - WHich end of queue tp pop from. |
|
inline |
Pop an item off the queue if there are any else wait.
| [out] | item | - The popped item, only valid if returns true. |
Method will block forever or until an item is placed on the queue.
This will throw std::runtime_error if there are no items on the queue when called.
|
inline |
Push an item onto the queue.
| [in] | item | - Object of type T to push onto queue. |
The arg item must be moveable either implicitly or by passing it to Push wrapped in a std::move() and item will be invalid once this function returns due to being moved onto the internal queue.
It is preferred to use this version when performance matters as move semantics are enforced.
|
inline |
Push an item onto the queue.
| [in] | item | - Object of type T to push onto queue. |
Use this version only when you want to push a copy of the item arg onto the innternal queue because maybe you want to use item afterwards.
This option is not as efficient as the first version of Push because move semantics are not enforced.
|
inline |
Size of the queue.
|
inline |
Take all items from the queue and return them, thus clearing down the internal queue.
If this method is called while a thread is blocking on the item event then due to interleving of calls and mutex lock timings this may cause the list to be cleared before a waiting pop function has had a chance to pop anything from the queue. This is safe but what it means is the pop function will not find anything to pop and will either throw or return false to indicate that nothing was popped off the queue.
|
inline |
Pop an item off the queue but only wait for a given amount of time.
| [in] | timeoutMilliseconds | - Amount of time to wait. |
| [out] | item | - The popped item, only valid if returns true. |
|
inline |
Pop an item off the queue but only wait for a given amount of time.
| [in] | timeoutMilliseconds | - Amount of time to wait. |
| [out] | item | - The popped item. |
If no items have been put onto the queue after the specified amount to time then a std::runtime_error exception is throw.
|
inline |
Pop an item off the queue if there are any else return.
| [out] | item | - The popped item, only valid if returns true. |
|
inline |
Pop an item off the queue.
| [out] | item | - The popped item. |
This will throw std::runtime_error if there are no items on the queue when called.
|
inline |
Steal an item from the back of the queue if there are any else return.
| [out] | item | - The stolen item, only valid if returns true. |
|
inline |
Steal an item from the back of the queue.
| [out] | item | - The stolen item. |
This will throw std::runtime_error if there are no items on the queue when called.
|
private |
Synchronization event.