Main Content

Use a Queue Block to Manage Messages

This example shows how to use a Queue block to store and queue messages. The Queue block allows you to specify message storage capacity, overwriting policy, and sorting policy during message transitions.

Manipulate Message Transitions Using Queue Sorting Policies

TheQueueblock supports three message sorting policies:

  • Last-in-first-out (LIFO) — The newest message in the storage departs first.

  • First-in-first-out (FIFO) — The oldest message in the storage departs first.

  • Priority — Messages are sorted based on their priority. The priority queue can be used only when theOverwrite the oldest element if queue is fullcheck box is cleared.

This example uses a simple message-based communication model that is introduced inAnimate and Understand Sending and Receiving Messages. The model includes Message Flow 1 and Message Flow 2 in which messages flow from Sine Wave1 to Scope1 and Sine Wave2 to Scope2.

Model Description

In Message Flow 1 and Message Flow 2:

  • TheSample timeparameter of Sine Wave1 and Sine Wave2 are set to0.1. They are identical sine wave sources.

  • TheSample time参数Receive1和发射e2 are set to0.5. They are identical Receive blocks.

  • The capacity of the Queue1 and Queue2 are100. Both queues have the same capacity.

  • The sorting policy of the Queue1 is后进先出and Queue2 isFIFO,which is displayed under the block labels. The only difference between two message flow scenarios is the queue sorting policy.

  • The signals among Sine Wave1, Receive1, and Receive2 are logged.

Simulate the Model and Review the Results

Simulate the model and observe the results in the Simulation Data Inspector.

Observe the output from Sine Wave1. The output from SineWave2 is not displayed because it is identical to SineWave1. Also observe that the signal from Receive1 is the representation of the sine wave but with longer intervals between samples. However, the signal from Receive2 is the first part of the sine wave reaching to the positive peak because the sorting policy of the Queue block is set toFIFOand its capacity is large enough to store messages.

Queue Block Message Overwriting Policies

You can also specify the Queue block message overwriting policy when the queue is full:

  • By default, theOverwrite the oldest element if queue is fullcheck box is selected. The block is set to always accept an incoming message, overwriting the oldest message in the storage. In this case, the block overwrites the oldest message, but the message departing the block is determined by the queue sorting policy.

In this example of queue behavior, messages are enumerated based on their arrival time at the FIFO and LIFO queues with capacity8. A new message with number9arrives. In FIFO and LIFO cases, message9replaces message 1 because it is the oldest element in the queue. However, observe the change of head and tail of the queue during this process. In the FIFO queue, message2departs first because it is the oldest message after message1is replaced. In the LIFO queue, message9departs first because it is the last message that arrives at the storage. After the message departure, a new message10arrives. The queue accepts the new messages to its empty storage bin.

  • If theOverwrite the oldest element if queue is fullcheck box is cleared, the icon of the Queue block changes and the block does not accept new messages when the queue is full. This is a blocking queue behavior.

Queue Block with Overwriting Policy Enabled

In theQueueSortingPoliciesModelSequenceViewermodel, the capacities of the Queue blocks in Message Flow 1 and Message Flow 2 are changed to15. Decreasing the capacity causes the incoming messages to overwrite the existing ones in the storage.

Simulate the model and open the Sequence Viewer block. In the Sequence Viewer block, scroll up or click theGo to first eventicon on the left. Observe the messages departing the block based on FIFO and LIFO policies.

Both Queue blocks have limited capacity. When their capacity is full, a new incoming message overwrites the oldest existing message in the queue. Scroll down and observe that the Sequence Viewer block displays the messages that overwrite the existing ones.

An Example of a Blocking Queue Behavior

Open theQueueOverWritingPolicyModelto inspect the blocking queue behavior.

In the Receive block, set theSample timeparameter to0.5.

Observe that in the Queue block:

  • TheOverwrite the oldest element if queue is fullcheck box is cleared. Observe the block icon change.

  • TheCapacityparameter is set to16.

  • In theStatisticstab, theNumber of entities departed, dandNumber of entities in block, ncheck boxes are selected.

Simulate the Model and Review Results

Simulate the model. Observe the warning displayed in the Diagnostic Viewer. Messages sent by the Send block were dropped during simulation.

The Queue block blocks the messages when theOverwrite the oldest element if queue is fullcheck box is cleared. You can increase the capacity of the Queue block to prevent message dropping.

Use Statistics to Analyze Queue Behavior

When theOverwrite the oldest element if queue is fullcheck box is cleared theStatisticstab is enabled. UseStatisticstab to enable output ports and observe the Queue block statistics. Statistics are not supported for code generation.

Observe the signals labeled Number of Messages in Block and Number of Messages Departed. Up to simulation time2,there are16messages in the storage, which is the queue capacity. After that, Number of Messages in Block takes values15and16because messages depart every0.5仿真时间和一个新的消息到来。

At simulation time10,a total of21messages departs the block.

Use Event Actions

You can also useEvent actions,when theOverwrite the oldest element if queue is fullcheck box is cleared. Event actions are not supported for code generation. For more information, seeEvents and Event Actions(SimEvents).

UseEvent actionsto specify the behavior of the message in certain events. For instance, theEntryand theExitactions are called just after the message entry and just before message exit. TheBlockedaction is called after a message is blocked.

For more information, seeModel a Message Receive Interface that Runs on Message Availability.

You can also model more complex communication policies by using blocks from the SimEvents® library, which requires a SimEvents® license.

See Also

|||||

Related Topics