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

The specified number of listeners can obtain elements from the queue. After all the listeners have obtained elements, those elements are deleted from the queue. More...

#include "nn/nlib/LockFree.h"

Public Types

typedef UniquePtr< const T, empty_func > DequeueType
 The type for the argument of Dequeue() and DequeueUnsafe().
 

Public Member Functions

 LockFreeBroadcastQueue () noexcept
 Instantiates the object with default parameters (default constructor).
 
 ~LockFreeBroadcastQueue () noexcept
 Destructor. More...
 
errno_t Init (size_t max_size, size_t listeners) noexcept
 Initializes the queue. This is not thread-safe. More...
 
errno_t Enqueue (const T *obj) noexcept
 Adds an element to the queue. This is thread-safe. More...
 
errno_t Dequeue (int32_t listener_id, DequeueType &obj) noexcept
 Specifies the listener and then reads an element from the queue. This is thread-safe when using a different listener_id. More...
 
size_t GetListenerCount () const noexcept
 Returns the number of listeners specified in Init(). This is thread-safe.
 

Detailed Description

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

The specified number of listeners can obtain elements from the queue. After all the listeners have obtained elements, those elements are deleted from the queue.

Template Parameters
TThe queue element type.
Description
Note that no objects that serve as the elements will be destroyed until all the listeners have obtained the elements.
Sample code
class BroadcastMsg {
public:
BroadcastMsg(const char* msg) {
size_t bufsize = nlib_strlen(msg) + 1;
msg_ = new char[bufsize];
nlib_strcpy(msg_, bufsize, msg);
}
~BroadcastMsg() { delete msg_; }
const char* Get() const { return msg_; }
private:
char* msg_;
};
.....
LockFreeBroadcastQueue<BroadcastMsg> queue;
queue.Init(128, 2);
queue.Enqueue(new BroadcastMsg("The messages are shared among the listeners."));
queue.Enqueue(new BroadcastMsg("They are preserved until all the listeners read them."));
queue.Enqueue(new BroadcastMsg("They are to be deleted automatically."));
......
{
// Thread 1
LockFreeBroadcastQueue<BroadcastMsg>::DequeueType ptr;
queue.Dequeue(0, ptr);
PrintMsgOnWindow1(ptr->Get());
queue.Dequeue(0, ptr);
PrintMsgOnWindow1(ptr->Get());
queue.Dequeue(0, ptr);
PrintMsgOnWindow1(ptr->Get());
}
......
{
// Thread 2
LockFreeBroadcastQueue<BroadcastMsg>::DequeueType ptr;
queue.Dequeue(0, ptr);
PrintMsgOnWindow2(ptr->Get());
queue.Dequeue(0, ptr);
PrintMsgOnWindow2(ptr->Get());
queue.Dequeue(0, ptr);
PrintMsgOnWindow2(ptr->Get());
}

Definition at line 726 of file LockFree.h.

Constructor & Destructor Documentation

template<class T >
nn::nlib::LockFreeBroadcastQueue< T >::~LockFreeBroadcastQueue ( )
inlinenoexcept

Destructor.

Description
Any elements remaining in the queue are destructed by DestructorForLockFree<const T>.

Definition at line 733 of file LockFree.h.

Member Function Documentation

template<class T >
nn::nlib::LockFreeBroadcastQueue< T >::Dequeue ( int32_t  listener_id,
DequeueType obj 
)
inlinenoexcept

Specifies the listener and then reads an element from the queue. This is thread-safe when using a different listener_id.

Parameters
[in]listener_idAn integer that is 0 or larger and less than the number of listeners specified in Init().
[out]objThe read element.
Return values
0Success.
EAGAINNo new element that can be read is available in the queue.
Description
The obtained elements are valid until Dequeue() is executed next time. Note that no element is deleted until Dequeue() for all listeners has completed.

Definition at line 748 of file LockFree.h.

template<class T >
nn::nlib::LockFreeBroadcastQueue< T >::Enqueue ( const T *  obj)
inlinenoexcept

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

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

Definition at line 744 of file LockFree.h.

template<class T >
nn::nlib::LockFreeBroadcastQueue< T >::Init ( size_t  max_size,
size_t  listeners 
)
inlinenoexcept

Initializes the queue. This is not thread-safe.

Parameters
[in]max_sizeThe maximum number of elements that can be stored in the queue.
[in]listenersThe number of listeners.
Return values
0Success.
EINVALmax_size exceeds INT32_MAX.
ENOMEMMemory allocation has failed.

Definition at line 740 of file LockFree.h.


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