Template message queue for inter-thread communication.
More...
#include <tsMessageQueue.h>
|
using | MessagePtr = std::shared_ptr< MSG > |
| Safe pointer to messages.
|
|
|
| MessageQueue (size_t maxMessages=0) |
| Constructor.
|
|
virtual | ~MessageQueue () |
| Destructor.
|
|
void | clear () |
| Clear the content of the queue.
|
|
void | dequeue (MessagePtr &msg) |
| Remove a message from the queue.
|
|
bool | dequeue (MessagePtr &msg, cn::milliseconds timeout) |
| Remove a message from the queue.
|
|
void | enqueue (MessagePtr &msg) |
| Insert a message in the queue.
|
|
bool | enqueue (MessagePtr &msg, cn::milliseconds timeout) |
| Insert a message in the queue.
|
|
void | enqueue (MSG *msg) |
| Insert a message in the queue.
|
|
bool | enqueue (MSG *msg, cn::milliseconds timeout) |
| Insert a message in the queue.
|
|
void | forceEnqueue (MessagePtr &msg) |
| Insert a message in the queue, even if the queue is full.
|
|
void | forceEnqueue (MSG *msg) |
| Insert a message in the queue, even if the queue is full.
|
|
size_t | getMaxMessages () const |
| Get the maximum allowed messages in the queue.
|
|
MessagePtr | peek () |
| Peek the next message from the queue, without dequeueing it.
|
|
void | setMaxMessages (size_t maxMessages) |
| Change the maximum allowed messages in the queue.
|
|
|
using | MessageList = std::list< MessagePtr > |
| Queues are implemented as list of smart pointers to messages.
|
|
template<typename MSG>
class ts::MessageQueue< MSG >
Template message queue for inter-thread communication.
The ts::MessageQueue template class implements a synchronized access to a shared queue of generic messages.
- Template Parameters
-
MSG | The type of the messages to exchange. |
◆ MessagePtr
Safe pointer to messages.
Since data are copied from the producer thread into the queue and later copied again from the queue into the consumer thread, the copied data is always a shared pointer to the actual message content.
◆ MessageQueue()
Constructor.
- Parameters
-
[in] | maxMessages | Maximum number of messages in the queue. |
- See also
- setMaxMessages()
◆ getMaxMessages()
Get the maximum allowed messages in the queue.
- Returns
- The maximum allowed messages in the queue (0 means unlimited).
◆ setMaxMessages()
Change the maximum allowed messages in the queue.
- Parameters
-
[in] | maxMessages | Maximum number of messages in the queue. When a thread attempts to enqueue a message and the queue is full, the thread waits until at least one message is dequeued. If maxMessages is 0, the queue is unlimited. In that case, the logic of the application must ensure that the queue is bounded somehow, otherwise the queue may exhaust all the process memory. |
◆ enqueue() [1/4]
Insert a message in the queue.
If the queue is full, the calling thread waits until some space becomes available in the queue.
- Parameters
-
[in,out] | msg | The message to enqueue. The ownership of the pointed object is transfered to the message queue. Upon return, the msg safe pointer becomes a null pointer if the message was successfully enqueued. |
◆ enqueue() [2/4]
Insert a message in the queue.
If the queue is full, the calling thread waits until some space becomes available in the queue or the timeout expires.
- Parameters
-
[in,out] | msg | The message to enqueue. The ownership of the pointed object is transfered to the message queue. Upon return, the msg safe pointer becomes a null pointer if the message was successfully enqueued (no timeout). |
[in] | timeout | Maximum time to wait in milliseconds. |
- Returns
- True on success, false on error (queue still full after timeout).
◆ enqueue() [3/4]
Insert a message in the queue.
- Parameters
-
[in] | msg | A pointer to the message to enqueue. This pointer shall not be owned by a safe pointer. When the message is successfully enqueued, the pointer becomes owned by a safe pointer and will be deallocated when no longer used. |
◆ enqueue() [4/4]
Insert a message in the queue.
- Parameters
-
[in] | msg | A pointer to the message to enqueue. This pointer shall not be owned by a safe pointer. When the message is successfully enqueued, the pointer becomes owned by a safe pointer and will be deallocated when no longer used. In case of timeout, the object is not equeued and immediately deallocated. |
[in] | timeout | Maximum time to wait in milliseconds. |
- Returns
- True on success, false on error (queue still full after timeout).
◆ forceEnqueue() [1/2]
Insert a message in the queue, even if the queue is full.
This method immediately inserts the message, even if the queue is full. This can be used to allow exceptional overflow of the queue with unique messages, to enqueue a message to instruct the consumer thread to terminate for instance.
- Parameters
-
[in,out] | msg | The message to enqueue. The ownership of the pointed object is transfered to the message queue. Upon return, the msg safe pointer becomes a null pointer. |
◆ forceEnqueue() [2/2]
Insert a message in the queue, even if the queue is full.
This method immediately inserts the message, even if the queue is full. This can be used to allow exceptional overflow of the queue with unique messages, to enqueue a message to instruct the consumer thread to terminate for instance.
- Parameters
-
[in] | msg | A pointer to the message to enqueue. This pointer shall not be owned by a safe pointer. When the message is enqueued, the pointer becomes owned by a safe pointer and will be deallocated when no longer used. |
◆ dequeue() [1/2]
Remove a message from the queue.
Wait until a message is received.
- Parameters
-
[out] | msg | Received message. |
◆ dequeue() [2/2]
Remove a message from the queue.
Wait until a message is received or the timeout expires.
- Parameters
-
[out] | msg | Received message. |
[in] | timeout | Maximum time to wait in milliseconds. If timeout is zero and the queue is empty, return immediately. |
- Returns
- True on success, false on error (queue still empty after timeout).
◆ peek()
Peek the next message from the queue, without dequeueing it.
If several threads simultaneously read from the queue, the returned message may be deqeued in the meantime by another thread.
- Returns
- A safe pointer to the first message in the queue or a null pointer if the queue is empty.
◆ enqueuePlacement()
This virtual protected method performs placement in the message queue.
- Parameters
-
[in] | msg | The message to enqueue later. The message is not enqueued. Its value is used to compute the place where it should be inserted. |
[in] | list | The content of the queue. |
- Returns
- An iterator to the place where msg shall be placed.
◆ dequeuePlacement()
This virtual protected method performs dequeue location in the message queue.
- Parameters
-
[in] | list | The content of the queue. |
- Returns
- An iterator to the place from where the next message shall be removed.
The documentation for this class was generated from the following file: