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"
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
-
- 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) {
msg_ = new char[bufsize];
}
~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."));
......
{
queue.Dequeue(0, ptr);
PrintMsgOnWindow1(ptr->Get());
queue.Dequeue(0, ptr);
PrintMsgOnWindow1(ptr->Get());
queue.Dequeue(0, ptr);
PrintMsgOnWindow1(ptr->Get());
}
......
{
queue.Dequeue(0, ptr);
PrintMsgOnWindow2(ptr->Get());
queue.Dequeue(0, ptr);
PrintMsgOnWindow2(ptr->Get());
queue.Dequeue(0, ptr);
PrintMsgOnWindow2(ptr->Get());
}
Definition at line 753 of file LockFree.h.
§ ~LockFreeBroadcastQueue()
Destructor.
- Description
- Any elements remaining in the queue are destructed by
DestructorForLockFree<const T>
.
Definition at line 760 of file LockFree.h.
§ Dequeue()
Specifies the listener and then reads an element from the queue. This is thread-safe when using a different listener_id.
- Parameters
-
[in] | listener_id | An integer that is 0 or larger and less than the number of listeners specified in Init() . |
[out] | obj | The read element. |
- Return values
-
0 | Success. |
EAGAIN | No 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 775 of file LockFree.h.
§ Enqueue()
Adds an element to the queue. This is thread-safe.
- Parameters
-
[in] | obj | The element to be added to the queue. |
- Return values
-
0 | Success. |
EAGAIN | The queue is full. |
Definition at line 771 of file LockFree.h.
§ Init()
Initializes the queue. This is not thread-safe.
- Parameters
-
[in] | max_size | The maximum number of elements that can be stored in the queue. |
[in] | listeners | The number of listeners. |
- Return values
-
0 | Success. |
EINVAL | max_size exceeds INT32_MAX . |
ENOMEM | Memory allocation has failed. |
Definition at line 767 of file LockFree.h.
The documentation for this class was generated from the following files: