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...
 

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 (e != 0) {
// ERROR
}
.....
MyClass* elem = new MyClass();
.....
e = queue.Enqueue(elem);
if (e != 0) {
// try again or ERROR
}
.....
LockFreeQueue<MyClass*>::DequeueType x;
e = queue.Dequeue(x);
if (e != 0) {
// try again or ERROR
}
.....
// x is automatically deleted

Definition at line 524 of file LockFree.h.

Member Typedef Documentation

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 526 of file LockFree.h.

Constructor & Destructor Documentation

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 528 of file LockFree.h.

Member Function Documentation

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 539 of file LockFree.h.

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 543 of file LockFree.h.

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 538 of file LockFree.h.

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 541 of file LockFree.h.

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 532 of file LockFree.h.


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