A wait-free queue for communication unidirectionally between two threads. More...
#include <WaitFreeQueue.h>
Public Member Functions | |
| WaitFreeQueue (size_t maxElements) | |
| int | Capacity () const |
| Returns the total capacity of the queue, i.e. | |
| int | CapacityLeft () const |
| Returns the number of items that can still be inserted in the queue, i.e. | |
| bool | Insert (const T &value) |
| Inserts the new value into the queue. Can be called only by a single producer thread. | |
| void | InsertWithResize (const T &value) |
| Inserts the new value into the queue. | |
| void | Resize (size_t newSize) |
| Re-allocates the queue to the new maximum size. | |
| void | DoubleCapacity () |
| Resizes this queue to hold twice the amount of maximum elements. | |
| T * | Front () |
| Returns a pointer to the first item in the queue. Can be called only from a single consumer thread. | |
| T * | ItemAt (int index) |
| Returns a pointer to the item at the given index. Can be called only from a single consumer thread. | |
| const T * | ItemAt (int index) const |
| Returns a pointer to the item at the given index. Can be called only from a single consumer thread. | |
| bool | Contains (const T &item) const |
| Returns true if the given element exists in the queue. Can be called only from a single consumer thread. | |
| void | EraseItemAt (int index) |
| Removes the element at the given index. | |
| const T * | Front () const |
| Returns a pointer to the first item in the queue. Can be called only from a single consumer thread. | |
| T | TakeFront () |
| Returns a copy of the first item in the queue and pops that item off the queue. Can be called only from a single consumer thread. | |
| void | Clear () |
| Removes all elements in the queue. | |
| unsigned long | Size () const |
| Returns the number of elements currently filled in the queue. Can be called from either the consumer or producer thread. | |
| void | PopFront () |
| Removes the first item in the queue. Can be called only from a single consumer thread. | |
A wait-free queue for communication unidirectionally between two threads.
This data structure is useful for simple and efficient lockless data/message passing between two-threads. It is implemented using a lockless circular ring buffer, and has the following properties:
| kNet::WaitFreeQueue< T >::WaitFreeQueue | ( | size_t | maxElements | ) | [inline, explicit] |
| maxElements | A power-of-2 number, > 2, that specifies the size of the ring buffer to construct. The number of elements the queue can store is maxElements-1. |
| int kNet::WaitFreeQueue< T >::Capacity | ( | ) | const [inline] |
Returns the total capacity of the queue, i.e.
the total maximum number of items that can be contained in it. Thread-safe.
Referenced by kNet::WaitFreeQueue< NetworkMessage * >::CapacityLeft().
| int kNet::WaitFreeQueue< T >::CapacityLeft | ( | ) | const [inline] |
Returns the number of items that can still be inserted in the queue, i.e.
the total space left. Thread-safe.
| void kNet::WaitFreeQueue< T >::InsertWithResize | ( | const T & | value | ) | [inline] |
Inserts the new value into the queue.
Referenced by kNet::MessageConnection::AcceptOutboundMessages(), kNet::MessageConnection::EndAndQueueMessage(), and kNet::MessageConnection::SplitAndQueueMessage().
| void kNet::WaitFreeQueue< T >::Resize | ( | size_t | newSize | ) | [inline] |
Re-allocates the queue to the new maximum size.
All old elements are copied over.
Referenced by kNet::WaitFreeQueue< NetworkMessage * >::DoubleCapacity().
| void kNet::WaitFreeQueue< T >::DoubleCapacity | ( | ) | [inline] |
Resizes this queue to hold twice the amount of maximum elements.
Referenced by kNet::WaitFreeQueue< NetworkMessage * >::InsertWithResize().
| void kNet::WaitFreeQueue< T >::EraseItemAt | ( | int | index | ) | [inline] |
Removes the element at the given index.
| void kNet::WaitFreeQueue< T >::Clear | ( | ) | [inline] |
Removes all elements in the queue.
Does not call dtors for removed objects, as this queue is only for POD types. Can be called only from a single consumer thread.
1.7.1