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

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

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 (e != 0) {
// ERROR
}
.....
MyClass* elem = new MyClass();
.....
e = stack.Push(elem);
if (e != 0) {
// try again or ERROR
}
.....
LockFreeStack<MyClass*>::PopType x;
e = stack.Pop(x);
if (e != 0) {
// try again or ERROR
}
.....
// x is automatically deleted

Definition at line 453 of file LockFree.h.

Member Typedef Documentation

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

Constructor & Destructor Documentation

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

Member Function Documentation

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

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

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

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

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


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