This class implements a lock-free stack.
More...
#include "nn/nlib/LockFree.h"
template<class T>
class nn::nlib::LockFreeStack< T >
This class implements a lock-free stack.
- Template Parameters
-
T | The 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.
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) {
}
}
});
}
for (auto& th : th_list) { th.join(); }
Pod pod;
for (int i = 0; i < kNumThread * 2; ++i) {
SUCCEED_IF(e == 0);
}
Definition at line 435 of file LockFree.h.
◆ PopType
◆ ~LockFreeStack()
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.
◆ Init()
Initializes the stack. This is not thread-safe.
- Parameters
-
[in] | count | The number of elements that can be placed in the stack. |
- Return values
-
0 | Success. |
ENOMEM | Memory allocation has failed. |
EALREADY | Already initialized. |
Definition at line 443 of file LockFree.h.
◆ Pop()
Picks up an element from the stack and stores it in x. This is thread-safe.
- Parameters
-
[in] | x | The region that stores elements picked up from the stack. |
- Return values
-
0 | Success. |
EAGAIN | The stack is empty. |
Definition at line 450 of file LockFree.h.
◆ PopUnsafe()
Picks up an element from the stack and stores it in x. This is not thread-safe.
- Parameters
-
[in] | x | The region that stores elements picked up from the stack. |
- Return values
-
0 | Success. |
EAGAIN | The stack is empty. |
Definition at line 454 of file LockFree.h.
◆ Push()
Places the element x in the stack. This is thread-safe.
- Parameters
-
[in] | x | The element to be placed in the stack. |
- Return values
-
0 | Success. |
EAGAIN | The stack is full. |
Definition at line 449 of file LockFree.h.
◆ PushUnsafe()
Places the element x in the stack. This is not thread-safe.
- Parameters
-
[in] | x | The element to be placed in the stack. |
- Return values
-
0 | Success. |
EAGAIN | The stack is full. |
Definition at line 452 of file LockFree.h.
The documentation for this class was generated from the following files: