This class implements a lock-free queue.
More...
#include "nn/nlib/LockFree.h"
template<class T>
class nn::nlib::LockFreeQueue< T >
This class implements a lock-free queue.
- Template Parameters
-
T | The 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. 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) {
}
}
});
}
for (auto& th : th_list) { th.join(); }
Pod pod;
for (int i = 0; i < kNumThread * 2; ++i) {
SUCCEED_IF(e == 0);
}
- See also
- http://www.cs.rochester.edu/~scott/papers/1996_PODC_queues.pdf
Definition at line 514 of file LockFree.h.
◆ DequeueType
◆ ~LockFreeQueue()
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 518 of file LockFree.h.
◆ Dequeue()
Picks up an element from the queue and stores it in x. This is thread-safe.
- Parameters
-
[in] | x | The region that stores elements picked up from the queue. |
- Return values
-
0 | Success. |
EAGAIN | The queue is empty. |
Definition at line 529 of file LockFree.h.
◆ DequeueUnsafe()
Picks up an element from the queue and stores it in x. This is not thread-safe.
- Parameters
-
[in] | x | The region that stores elements picked up from the queue. |
- Return values
-
0 | Success. |
EAGAIN | The queue is empty. |
Definition at line 533 of file LockFree.h.
◆ Enqueue()
Adds the element x to the queue. This is thread-safe.
- Parameters
-
[in] | x | The element to be added to the queue. |
- Return values
-
0 | Success. |
EAGAIN | The queue is full. |
Definition at line 528 of file LockFree.h.
◆ EnqueueUnsafe()
Adds the element x to the queue. This is not thread-safe.
- Parameters
-
[in] | x | The element to be added to the queue. |
- Return values
-
0 | Success. |
EAGAIN | The queue is full. |
Definition at line 531 of file LockFree.h.
◆ Init()
Initializes the queue. This is not thread-safe.
- Parameters
-
[in] | count | The number of elements that can be added to the queue. |
- Return values
-
0 | Success. |
ENOMEM | Memory allocation has failed. |
EALREADY | Already initialized. |
Definition at line 522 of file LockFree.h.
The documentation for this class was generated from the following files: