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

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

#include "nn/nlib/LockFree.h"

Public Types

typedef T * DequeueType
 The type for the argument of Dequeue() and DequeueUnsafe(). More...
 

Public Member Functions

errno_t Enqueue (const T &x) noexcept
 Adds the element x to the queue. This is thread-safe. More...
 
errno_t Dequeue (DequeueType x) noexcept
 Picks up an element from the queue and stores it in x. This is thread-safe. More...
 
errno_t EnqueueUnsafe (const T &x) noexcept
 Adds the element x to the queue. This is not thread-safe. More...
 
errno_t DequeueUnsafe (DequeueType x) noexcept
 Picks up an element from the queue and stores it in x. This is not thread-safe. More...
 
void SwapUnsafe (LockFreeQueue &rhs) noexcept
 Swaps an object. This is not thread-safe.
 
Constructor, Destructor, and Initialization
 LockFreeQueue () noexcept
 Instantiates the object with default parameters (default constructor).
 
 ~LockFreeQueue () noexcept
 Destructor. More...
 
errno_t Init (size_t count) noexcept
 Initializes the queue. This is not thread-safe. More...
 

Detailed Description

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

This class implements a lock-free queue.

Template Parameters
TThe 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.
struct Pod { nlib_thread_id threadid; };
errno_t e = queue.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 (queue.Enqueue(pod) != 0) {
}
nlib_sleep(10000);
}
});
}
for (auto& th : th_list) { th.join(); }
Pod pod;
for (int i = 0; i < kNumThread * 2; ++i) {
e = queue.DequeueUnsafe(&pod); // you can use DequeueUnsafe/DequeueUnsafe if single threaded
SUCCEED_IF(e == 0);
nlib_printf("dequeue: threadid = %d\n", pod.threadid);
}
SUCCEED_IF(queue.DequeueUnsafe(&pod) == EAGAIN); // empty
/*
Output:
dequeue: threadid = 18992
dequeue: threadid = 10128
dequeue: threadid = 19896
dequeue: threadid = 12040
dequeue: threadid = 10120
dequeue: threadid = 23808
dequeue: threadid = 4872
dequeue: threadid = 19440
dequeue: threadid = 19896
dequeue: threadid = 4872
dequeue: threadid = 19440
dequeue: threadid = 10128
dequeue: threadid = 23808
dequeue: threadid = 18992
dequeue: threadid = 12040
dequeue: threadid = 10120
*/
See also
http://www.cs.rochester.edu/~scott/papers/1996_PODC_queues.pdf

Definition at line 514 of file LockFree.h.

Member Typedef Documentation

◆ DequeueType

template<class T>
nn::nlib::LockFreeQueue< T >::DequeueType

The type for the argument of Dequeue() and DequeueUnsafe().

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

Definition at line 516 of file LockFree.h.

Constructor & Destructor Documentation

◆ ~LockFreeQueue()

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

Member Function Documentation

◆ Dequeue()

template<class T>
nn::nlib::LockFreeQueue< T >::Dequeue ( DequeueType  x)
inlinenoexcept

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

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

Definition at line 529 of file LockFree.h.

◆ DequeueUnsafe()

template<class T>
nn::nlib::LockFreeQueue< T >::DequeueUnsafe ( DequeueType  x)
inlinenoexcept

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

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

Definition at line 533 of file LockFree.h.

◆ Enqueue()

template<class T>
nn::nlib::LockFreeQueue< T >::Enqueue ( const T &  x)
inlinenoexcept

Adds the element x to the queue. This is thread-safe.

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

Definition at line 528 of file LockFree.h.

◆ EnqueueUnsafe()

template<class T>
nn::nlib::LockFreeQueue< T >::EnqueueUnsafe ( const T &  x)
inlinenoexcept

Adds the element x to the queue. This is not thread-safe.

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

Definition at line 531 of file LockFree.h.

◆ Init()

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

Initializes the queue. This is not thread-safe.

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

Definition at line 522 of file LockFree.h.


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