nlib
nn::nlib::LockFreeQueue< T > Class Template Reference

This class implements a lock-free queue. More...

#include "nn/nlib/LockFree.h"

Public Types

typedef T * DequeueType
 The type for the argument of Dequeue() and DequeueUnsafe(). More...
 

Public Member Functions

 LockFreeQueue () noexcept
 Instantiates the object with default parameters (default constructor).
 
 ~LockFreeQueue () noexcept
 Destructor. More...
 
errno_t Init (size_t count) noexcept
 Initializes the queue. This is not thread-safe. More...
 
errno_t Enqueue (const T &x) noexcept
 Adds the element x to the queue. This is thread-safe. More...
 
errno_t Dequeue (DequeueType x) noexcept
 Picks up an element from the queue and stores it in x. This is thread-safe. More...
 
errno_t EnqueueUnsafe (const T &x) noexcept
 Adds the element x to the queue. This is not thread-safe. More...
 
errno_t DequeueUnsafe (DequeueType x) noexcept
 Picks up an element from the queue and stores it in x. This is not thread-safe. More...
 
void SwapUnsafe (LockFreeQueue &rhs) noexcept
 Swaps an object. This is not thread-safe.
 

Detailed Description

template<class T>
class nn::nlib::LockFreeQueue< T >

This class implements a lock-free queue.

Template Parameters
TThe queue element type. This type must be POD.
Description
The Enqueue() and Dequeue() member functions are thread-safe. If you specify a pointer type for T, executing DestructorForLockFree deletes objects of the T type added to the queue.
Sample code
LockFreeQueue<MyClass*> queue;
e = queue.Init(1024);
if (nlib_is_error(e)) {
// ERROR
}
.....
MyClass* elem = new MyClass();
.....
e = queue.Enqueue(elem);
if (nlib_is_error(e)) {
// try again or ERROR
}
.....
LockFreeQueue<MyClass*>::DequeueType x;
e = queue.Dequeue(x);
if (nlib_is_error(e)) {
// try again or ERROR
}
.....
// x is automatically deleted
See also
http://www.cs.rochester.edu/~scott/papers/1996_PODC_queues.pdf

Definition at line 542 of file LockFree.h.

Member Typedef Documentation

◆ DequeueType

template<class T>
nn::nlib::LockFreeQueue< T >::DequeueType

The type for the argument of Dequeue() and DequeueUnsafe().

Description
When T is a pointer type, the typedef defines UniquePtr so that executing DestructorForLockFree automatically destructs object of that type.

Definition at line 544 of file LockFree.h.

Constructor & Destructor Documentation

◆ ~LockFreeQueue()

template<class T>
nn::nlib::LockFreeQueue< T >::~LockFreeQueue ( )
inlinenoexcept

Destructor.

Description
Uses DestructorForLockFree to delete each of remaining elements, if any. If necessary, specialize a function template to perform necessary processing.

Definition at line 546 of file LockFree.h.

Member Function Documentation

◆ Dequeue()

template<class T>
nn::nlib::LockFreeQueue< T >::Dequeue ( DequeueType  x)
inlinenoexcept

Picks up an element from the queue and stores it in x. This is thread-safe.

Parameters
[in]xThe region that stores elements picked up from the queue.
Return values
0Success.
EAGAINThe queue is empty.

Definition at line 557 of file LockFree.h.

◆ DequeueUnsafe()

template<class T>
nn::nlib::LockFreeQueue< T >::DequeueUnsafe ( DequeueType  x)
inlinenoexcept

Picks up an element from the queue and stores it in x. This is not thread-safe.

Parameters
[in]xThe region that stores elements picked up from the queue.
Return values
0Success.
EAGAINThe queue is empty.

Definition at line 561 of file LockFree.h.

◆ Enqueue()

template<class T>
nn::nlib::LockFreeQueue< T >::Enqueue ( const T &  x)
inlinenoexcept

Adds the element x to the queue. This is thread-safe.

Parameters
[in]xThe element to be added to the queue.
Return values
0Success.
EAGAINThe queue is full.

Definition at line 556 of file LockFree.h.

◆ EnqueueUnsafe()

template<class T>
nn::nlib::LockFreeQueue< T >::EnqueueUnsafe ( const T &  x)
inlinenoexcept

Adds the element x to the queue. This is not thread-safe.

Parameters
[in]xThe element to be added to the queue.
Return values
0Success.
EAGAINThe queue is full.

Definition at line 559 of file LockFree.h.

◆ Init()

template<class T>
nn::nlib::LockFreeQueue< T >::Init ( size_t  count)
inlinenoexcept

Initializes the queue. This is not thread-safe.

Parameters
[in]countThe number of elements that can be added to the queue.
Return values
0Success.
ENOMEMMemory allocation has failed.
EALREADYAlready initialized.

Definition at line 550 of file LockFree.h.


The documentation for this class was generated from the following files: