Games Task Scheduler (GTS)
A multi-processor scheduling framework for games engines
gts::QueueMPMC< T, TMutex, TAllocator > Class Template Reference

A multi-producer, multi-consumer queue. Properties: More...

#include <QueueMPMC.h>

Inherits gts::AlignedAllocator< GTS_NO_SHARING_CACHE_LINE_SIZE >.

Public Types

using queue_type = internal::TicketQueueMPMC< T, TMutex, TAllocator >
 
using value_type = typename queue_type::value_type
 
using mutex_type = typename queue_type::mutex_type
 
using size_type = typename queue_type::size_type
 
using allocator_type = typename queue_type::allocator_type
 

Public Member Functions

 QueueMPMC (size_t numSubQueuesPow2=Thread::getHardwareThreadCount(), allocator_type const &allocator=allocator_type())
 
 ~QueueMPMC ()
 
 QueueMPMC (QueueMPMC const &other)
 
 QueueMPMC (QueueMPMC &&other)
 
QueueMPMCoperator= (QueueMPMC const &other)
 
QueueMPMCoperator= (QueueMPMC &&other)
 
bool empty () const
 
size_type size () const
 
size_type capacity () const
 
allocator_type get_allocator () const
 
void reserve (size_type sizePow2)
 
void clear ()
 
bool tryPush (const value_type &val)
 
bool tryPush (value_type &&val)
 
bool tryPop (value_type &out)
 

Detailed Description

template<typename T, typename TMutex = UnfairSpinMutex<>, typename TAllocator = AlignedAllocator<GTS_NO_SHARING_CACHE_LINE_SIZE>>
class gts::QueueMPMC< T, TMutex, TAllocator >

A multi-producer, multi-consumer queue. Properties:

  • Unbound.
  • Distributed to reduce contention.
    Template Parameters
    TThe type stored in the container.
    TMutexThe mutex type that guards access to the container.
    TAllocatorThe allocator type used for the container.
    TSizeThe integral type used for the container size.

Constructor & Destructor Documentation

◆ QueueMPMC() [1/3]

template<typename T , typename TMutex , typename TAllocator >
gts::QueueMPMC< T, TMutex, TAllocator >::QueueMPMC ( size_t  numSubQueuesPow2 = Thread::getHardwareThreadCount(),
allocator_type const &  allocator = allocator_type() 
)
explicit

Constructs an empty container with 'numSubQueuesPow2' sub vectors and with the given 'allocator'.

Remarks
Thread-safe.

◆ ~QueueMPMC()

template<typename T , typename TMutex , typename TAllocator >
gts::QueueMPMC< T, TMutex, TAllocator >::~QueueMPMC

Destructs the container. The destructors of the elements are called and the used storage is deallocated.

Remarks
Not thread-safe.

◆ QueueMPMC() [2/3]

template<typename T , typename TMutex , typename TAllocator >
gts::QueueMPMC< T, TMutex, TAllocator >::QueueMPMC ( QueueMPMC< T, TMutex, TAllocator > const &  other)

Copy constructor. Constructs the container with the copy of the contents of 'other'.

Remarks
Not thread-safe.

◆ QueueMPMC() [3/3]

template<typename T , typename TMutex , typename TAllocator >
gts::QueueMPMC< T, TMutex, TAllocator >::QueueMPMC ( QueueMPMC< T, TMutex, TAllocator > &&  other)

Move constructor. Constructs the container with the contents of other using move semantics. After the move, other is invalid.

Remarks
Not thread-safe.

Member Function Documentation

◆ capacity()

template<typename T , typename TMutex , typename TAllocator >
QueueMPMC< T, TMutex, TAllocator >::size_type gts::QueueMPMC< T, TMutex, TAllocator >::capacity
Returns
The capacity of the queue.
Remarks
Not thread-safe.

◆ clear()

template<typename T , typename TMutex , typename TAllocator >
void gts::QueueMPMC< T, TMutex, TAllocator >::clear

Removes all elements from the queue.

Remarks
Not thread-safe.

◆ empty()

template<typename T , typename TMutex , typename TAllocator >
bool gts::QueueMPMC< T, TMutex, TAllocator >::empty
Returns
True of the queue is empty, false otherwise.
Remarks
Not thread-safe.

◆ get_allocator()

template<typename T , typename TMutex , typename TAllocator >
QueueMPMC< T, TMutex, TAllocator >::allocator_type gts::QueueMPMC< T, TMutex, TAllocator >::get_allocator
Returns
This queue's allocator.
Remarks
Thread-safe.

◆ operator=() [1/2]

template<typename T , typename TMutex , typename TAllocator >
QueueMPMC< T, TMutex, TAllocator > & gts::QueueMPMC< T, TMutex, TAllocator >::operator= ( QueueMPMC< T, TMutex, TAllocator > &&  other)

Move assignment operator. Replaces the contents with those of other using move semantics. After the move, other is invalid.

Remarks
Not thread-safe.

◆ operator=() [2/2]

template<typename T , typename TMutex , typename TAllocator >
QueueMPMC< T, TMutex, TAllocator > & gts::QueueMPMC< T, TMutex, TAllocator >::operator= ( QueueMPMC< T, TMutex, TAllocator > const &  other)

Copy assignment operator. Replaces the contents with a copy of the contents of 'other'.

Remarks
Not thread-safe.

◆ reserve()

template<typename T , typename TMutex , typename TAllocator >
void gts::QueueMPMC< T, TMutex, TAllocator >::reserve ( size_type  sizePow2)

Increases the capacity of the queue. Does nothing if 'sizePow2' < capacity.

Remarks
Not thread-safe.

◆ size()

template<typename T , typename TMutex , typename TAllocator >
QueueMPMC< T, TMutex, TAllocator >::size_type gts::QueueMPMC< T, TMutex, TAllocator >::size
Returns
The number of elements in the queue.
Remarks
Not thread-safe.

◆ tryPop()

template<typename T , typename TMutex , typename TAllocator >
bool gts::QueueMPMC< T, TMutex, TAllocator >::tryPop ( value_type &  out)

Pops an element from the queue and copies it into 'out'.

Returns
True if the pop succeeded, false otherwise.
Remarks
Thread-safe.

◆ tryPush() [1/2]

template<typename T , typename TMutex , typename TAllocator >
bool gts::QueueMPMC< T, TMutex, TAllocator >::tryPush ( const value_type &  val)

Copies 'val' into the queue.

Returns
True if the pop push, false otherwise.
Remarks
Thread-safe.

◆ tryPush() [2/2]

template<typename T , typename TMutex , typename TAllocator >
bool gts::QueueMPMC< T, TMutex, TAllocator >::tryPush ( value_type &&  val)

Moves 'val' into the queue.

Returns
True if the pop push, false otherwise.
Remarks
Thread-safe.