nlib
nn::nlib::LockFreeStack< T > Class Template Referencefinal

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

#include "nn/nlib/LockFree.h"

Public Types

typedef T * PopType
 The type for the argument of Pop() and PopUnsafe(). More...
 

Public Member Functions

 LockFreeStack () noexcept
 Instantiates the object with default parameters (default constructor).
 
 ~LockFreeStack () noexcept
 Destructor. More...
 
errno_t Init (size_t count) noexcept
 Initializes the stack. This is not thread-safe. More...
 
errno_t Push (const T &x) noexcept
 Places the element x in the stack. This is thread-safe. More...
 
errno_t Pop (PopType x) noexcept
 Picks up an element from the stack and stores it in x. This is thread-safe. More...
 
errno_t PushUnsafe (const T &x) noexcept
 Places the element x in the stack. This is not thread-safe. More...
 
errno_t PopUnsafe (PopType x) noexcept
 Picks up an element from the stack and stores it in x. This is not thread-safe. More...
 
void SwapUnsafe (LockFreeStack &rhs) noexcept
 Swaps an object. This is not thread-safe.
 

Detailed Description

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

This class implements a lock-free stack.

Template Parameters
TThe type of elements to be placed in a stack. This type must be POD.
Description
The Push() and Pop() member functions are thread-safe. If you specify a pointer type for T, executing DestructorForLockFree automatically deletes objects of the T type placed in the stack.
Sample code
LockFreeStack<MyClass*> stack;
e = stack.Init(1024);
if (nlib_is_error(e)) {
// ERROR
}
.....
MyClass* elem = new MyClass();
.....
e = stack.Push(elem);
if (nlib_is_error(e)) {
// try again or ERROR
}
.....
LockFreeStack<MyClass*>::PopType x;
e = stack.Pop(x);
if (nlib_is_error(e)) {
// try again or ERROR
}
.....
// x is automatically deleted

Definition at line 463 of file LockFree.h.

Member Typedef Documentation

◆ PopType

template<class T >
nn::nlib::LockFreeStack< T >::PopType

The type for the argument of Pop() and PopUnsafe().

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

Definition at line 465 of file LockFree.h.

Constructor & Destructor Documentation

◆ ~LockFreeStack()

template<class T >
nn::nlib::LockFreeStack< T >::~LockFreeStack ( )
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 467 of file LockFree.h.

Member Function Documentation

◆ Init()

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

Initializes the stack. This is not thread-safe.

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

Definition at line 471 of file LockFree.h.

◆ Pop()

template<class T >
nn::nlib::LockFreeStack< T >::Pop ( PopType  x)
inlinenoexcept

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

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

Definition at line 478 of file LockFree.h.

◆ PopUnsafe()

template<class T >
nn::nlib::LockFreeStack< T >::PopUnsafe ( PopType  x)
inlinenoexcept

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

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

Definition at line 482 of file LockFree.h.

◆ Push()

template<class T >
nn::nlib::LockFreeStack< T >::Push ( const T &  x)
inlinenoexcept

Places the element x in the stack. This is thread-safe.

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

Definition at line 477 of file LockFree.h.

◆ PushUnsafe()

template<class T >
nn::nlib::LockFreeStack< T >::PushUnsafe ( const T &  x)
inlinenoexcept

Places the element x in the stack. This is not thread-safe.

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

Definition at line 480 of file LockFree.h.


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