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

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.
 
Constructor, Destructor, and Initialization
 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...
 

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 is provided below.
struct Pod { nlib_thread_id threadid; };
errno_t e = stack.Init(128);
SUCCEED_IF(e == 0);
const int kNumThread = 8;
std::thread th_list[kNumThread];
for (auto& th : th_list) {
th = std::thread([&]() {
Pod pod;
pod.threadid = GetMyThreadId();
for (int i = 0; i < 2; ++i) {
while (stack.Push(pod) != 0) {
}
nlib_sleep(10000);
}
});
}
for (auto& th : th_list) { th.join(); }
Pod pod;
for (int i = 0; i < kNumThread * 2; ++i) {
e = stack.PopUnsafe(&pod); // you can use PushUnsafe/PopUnsafe if single threaded
SUCCEED_IF(e == 0);
nlib_printf("pop: threadid = %d\n", pod.threadid);
}
SUCCEED_IF(stack.PopUnsafe(&pod) == EAGAIN); // empty
/*
Output:
pop: threadid = 24248
pop: threadid = 14012
pop: threadid = 21972
pop: threadid = 7384
pop: threadid = 17956
pop: threadid = 19628
pop: threadid = 21608
pop: threadid = 16848
pop: threadid = 24248
pop: threadid = 14012
pop: threadid = 17956
pop: threadid = 19628
pop: threadid = 7384
pop: threadid = 21972
pop: threadid = 21608
pop: threadid = 16848
*/

Definition at line 435 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 437 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 439 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 443 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 450 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 454 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 449 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 452 of file LockFree.h.


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