| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → SimEvents |
| Contents | Index |
| Learn more about SimEvents |
| On this page… |
|---|
This example (open model) shows how you can vary the processing sequence for simultaneous events by varying their event priorities. The example creates race conditions at a switch and illustrates multiple ways to resolve the race conditions.

At T=1, 2, 3,... the Repeating Sequence Stair block changes its value from 1 to 2 or from 2 to 1. The change causes the following events to occur, not necessarily in this sequence:
The top entity generator generates an entity.
The bottom entity generator generates an entity.
The Input Switch block selects a different entity input port.
Both entity generators are configured so that if a generated entity cannot depart immediately, the generator holds the entity and temporarily suspends the generation of additional entities.
In the model, the two Set Attribute blocks assign a Source attribute to each entity. The attribute value is 1 or 2 depending on which entity generator generated the entity. The Attribute Scope block plots the Source attribute values to indicate the source of each entity that departs from the switch.
If the two entity generators and the switch all have the Resolve simultaneous signal updates according to event priority option turned off, then you cannot necessarily predict the sequence in which the blocks schedule their reactions to changes in the output signal from the Repeating Sequence Stair block.
The rest of this example assumes that the two entity generators and the switch all use the Resolve simultaneous signal updates according to event priority option, for greater control over the sequencing of simultaneous events.
Suppose the two entity generators and the switch have the explicit event priorities shown.
| Event Type | Event Priority |
|---|---|
| Generation event at top entity generator | 300 |
| Generation event at bottom entity generator | 310 |
| Port selection event at switch | 200 |
The following describes what happens at T=1, 2, 3 using messages from the simulation log of the SimEvents debugger. To learn more about the debugger, see Overview of the SimEvents Debugger.
The output signal from the Repeating Sequence Stair block changes from 1 to 2 and blocks that connect to it detect the relevant update. The message from the Input Switch block indicates the previous and new values.
%==============================================================================% Detected Either Value Change Time = 1.000000000000000 : NewValue = 2 : PrevValue = 1 : Block = Input Switch
The blocks that react to the update then schedule events on the event calendar.
%------------------------------------------------------------------------------%
Events in the Event Calendar
ID EventTime EventType Priority Entity Block
ev4 1.000000000000000 PortSelection 200 <none> Input Switch
ev5 1.000000000000000 EntityGeneration 300 <none> Event-Based Entity Generator1
ev6 1.000000000000000 EntityGeneration 310 <none> Event-Based Entity Generator2The switch selects its IN2 entity input port.
%==============================================================================% Executing PortSelection Event (ev4) Time = 1.000000000000000 : Entity = <none> Priority = 200 : Block = Input Switch
The top entity generator generates an entity, which cannot depart because the switch's IN1 entity input port is unavailable.
%==============================================================================%
Executing EntityGeneration Event (ev5) Time = 1.000000000000000
: Entity = <none> Priority = 300
: Block = Event-Based Entity Generator1
%..........................................................................%
Generating Entity (en1)
: Block = Event-Based Entity Generator1The bottom entity generator generates an entity. This entity advances from block to block until it reaches the Attribute Scope block, which destroys it.
%==============================================================================%
Executing EntityGeneration Event (ev6) Time = 1.000000000000000
: Entity = <none> Priority = 310
: Block = Event-Based Entity Generator2
%..........................................................................%
Generating Entity (en2)
: Block = Event-Based Entity Generator2
%..........................................................................%
Entity Advancing (en2)
: From = Event-Based Entity Generator2
: To = Set Attribute2
%..........................................................................%
[...other messages...]
%..........................................................................%
Entity Advancing (en2)
: From = Get Attribute
: To = Attribute Scope
%..........................................................................%
Destroying Entity (en2)
: Block = Attribute ScopeBlocks detect the next relevant update in the output signal from the Repeating Sequence Stair block, and react by scheduling events.
%------------------------------------------------------------------------------%
Events in the Event Calendar
ID EventTime EventType Priority Entity Block
ev8 2.000000000000000 PortSelection 200 <none> Input Switch
ev9 2.000000000000000 EntityGeneration 300 <none> Event-Based Entity Generator1
ev10 2.000000000000000 EntityGeneration 310 <none> Event-Based Entity Generator2The switch selects its IN1 entity input port. This causes the top entity generator to output the entity it generated 1 second ago. This entity advances from block to block until it reaches the Attribute Scope block.
%==============================================================================%
Executing PortSelection Event (ev8) Time = 2.000000000000000
: Entity = <none> Priority = 200
: Block = Input Switch
%..........................................................................%
Entity Advancing (en1)
: From = Event-Based Entity Generator1
: To = Set Attribute1
%..........................................................................%
[...other messages...]
%..........................................................................%
Entity Advancing (en1)
: From = Get Attribute
: To = Attribute Scope
%..........................................................................%
Destroying Entity (en1)
: Block = Attribute ScopeThe top entity generator generates an entity, which advances from block to block until it reaches the Attribute Scope block. A total of two entities from the top entity generator reach the scope at this time instant.
%==============================================================================%
Executing EntityGeneration Event (ev9) Time = 2.000000000000000
: Entity = <none> Priority = 300
: Block = Event-Based Entity Generator1
%..........................................................................%
Generating Entity (en3)
: Block = Event-Based Entity Generator1
%..........................................................................%
Entity Advancing (en3)
: From = Event-Based Entity Generator1
: To = Set Attribute1
%..........................................................................%
[...other messages...]
%..........................................................................%
Entity Advancing (en3)
: From = Get Attribute
: To = Attribute Scope
%..........................................................................%
Destroying Entity (en3)
: Block = Attribute ScopeThe bottom entity generator generates an entity, which cannot depart because the switch's IN2 entity input port is unavailable.
%==============================================================================%
Executing EntityGeneration Event (ev10) Time = 2.000000000000000
: Entity = <none> Priority = 310
: Block = Event-Based Entity Generator2
sedebug>>step over
%..........................................................................%
Generating Entity (en4)
: Block = Event-Based Entity Generator2Blocks detect the next relevant update in the output signal from the Repeating Sequence Stair block, and react by scheduling events.
%------------------------------------------------------------------------------%
Events in the Event Calendar
ID EventTime EventType Priority Entity Block
ev13 3.000000000000000 PortSelection 200 <none> Input Switch
ev14 3.000000000000000 EntityGeneration 300 <none> Event-Based Entity Generator1
ev15 3.000000000000000 EntityGeneration 310 <none> Event-Based Entity Generator2The switch selects its IN2 entity input port. This causes the bottom entity generator to output the entity it generated 1 second ago. This entity advances from block to block until it reaches the Attribute Scope block.
%==============================================================================%
Executing PortSelection Event (ev13) Time = 3.000000000000000
: Entity = <none> Priority = 200
: Block = Input Switch
%..........................................................................%
Entity Advancing (en4)
: From = Event-Based Entity Generator2
: To = Set Attribute2
%..........................................................................%
[...other messages...]
%..........................................................................%
Entity Advancing (en4)
: From = Get Attribute
: To = Attribute Scope
%..........................................................................%
Destroying Entity (en4)
: Block = Attribute Scope
The top entity generator generates an entity, which cannot depart because the switch's IN1 entity input port is unavailable.
%==============================================================================%
Executing EntityGeneration Event (ev14) Time = 3.000000000000000
: Entity = <none> Priority = 300
: Block = Event-Based Entity Generator1
%..........................................................................%
Generating Entity (en5)
: Block = Event-Based Entity Generator1The bottom entity generator generates an entity, which advances from block to block until it reaches the Attribute Scope block. A total of two entities from the bottom entity generator reach the scope at this time instant.
%==============================================================================%
Executing EntityGeneration Event (ev15) Time = 3.000000000000000
: Entity = <none> Priority = 310
: Block = Event-Based Entity Generator2
%..........................................................................%
Generating Entity (en6)
: Block = Event-Based Entity Generator2
%..........................................................................%
Entity Advancing (en6)
: From = Event-Based Entity Generator2
: To = Set Attribute2
%..........................................................................%
[...other messages...]
%..........................................................................%
Entity Advancing (en6)
: From = Get Attribute
: To = Attribute Scope
%..........................................................................%
Destroying Entity (en6)
: Block = Attribute Scope
The plot of entities' Source attribute values shows an alternating pattern of dots, as does the plot of the port selection signal p. The list of times and values of the entities' Source attribute, as recorded in the Source_attr variable in the MATLAB workspace, shows that two entities from the same entity generator reach the scope at T=2, 3, 4, etc.
Port Selection Signal

Switch Departures When Port Selection Is Processed First

[Source_attr.time, Source_attr.signals.values]
ans =
1 2
2 1
2 1
3 2
3 2
4 1
4 1
5 2
5 2
6 1
6 1
7 2
7 2
8 1
8 1
9 2
9 2
10 1
10 1Suppose the two entity generators and the switch have the explicit event priorities shown below.
| Event Type | Event Priority |
|---|---|
| Generation event at top entity generator | 300 |
| Generation event at bottom entity generator | 310 |
| Port selection event at switch | 4000 |
At the beginning of the simulation, the port selection signal, p, is 1.
The output signal from the Repeating Sequence Stair block changes from 1 to 2 and blocks that connect to it detect the relevant update. The message from the Input Switch block indicates the previous and new values.
%==============================================================================% Detected Either Value Change Time = 1.000000000000000 : NewValue = 2 : PrevValue = 1 : Block = Input Switch
The blocks that react to the update then schedule events on the event calendar.
%------------------------------------------------------------------------------%
Events in the Event Calendar
ID EventTime EventType Priority Entity Block
ev5 1.000000000000000 EntityGeneration 300 <none> Event-Based Entity Generator1
ev6 1.000000000000000 EntityGeneration 310 <none> Event-Based Entity Generator2
ev4 1.000000000000000 PortSelection 4000 <none> Input SwitchThe top entity generator generates an entity. This entity advances from block to block until it reaches the Attribute Scope block, which destroys it.
%==============================================================================%
Executing EntityGeneration Event (ev5) Time = 1.000000000000000
: Entity = <none> Priority = 300
: Block = Event-Based Entity Generator1
%..........................................................................%
Generating Entity (en1)
: Block = Event-Based Entity Generator1
%..........................................................................%
Entity Advancing (en1)
: From = Event-Based Entity Generator1
: To = Set Attribute1
%..........................................................................%
[...other messages...]
%..........................................................................%
Entity Advancing (en1)
: From = Get Attribute
: To = Attribute Scope
The bottom entity generator generates an entity, which cannot depart because the switch's IN2 entity input port is unavailable.
%==============================================================================%
Executing EntityGeneration Event (ev6) Time = 1.000000000000000
: Entity = <none> Priority = 310
: Block = Event-Based Entity Generator2
%..........................................................................%
Generating Entity (en2)
: Block = Event-Based Entity Generator2
The switch selects its IN2 entity input port. This causes the bottom entity generator to output the entity it just generated. This entity advances from block to block until it reaches the Attribute Scope block.
%==============================================================================%
Executing PortSelection Event (ev4) Time = 1.000000000000000
: Entity = <none> Priority = 4000
: Block = Input Switch
%..........................................................................%
Entity Advancing (en2)
: From = Event-Based Entity Generator2
: To = Set Attribute2
%..........................................................................%
[...other messages...]
%..........................................................................%
Entity Advancing (en2)
: From = Get Attribute
: To = Attribute ScopeBlocks detect the next relevant update in the output signal from the Repeating Sequence Stair block, and react by scheduling events.
%------------------------------------------------------------------------------%
Events in the Event Calendar
ID EventTime EventType Priority Entity Block
ev10 2.000000000000000 EntityGeneration 300 <none> Event-Based Entity Generator1
ev11 2.000000000000000 EntityGeneration 310 <none> Event-Based Entity Generator2
ev9 2.000000000000000 PortSelection 4000 <none> Input Switch
The top entity generator generates an entity, which cannot depart because the switch's IN1 entity input port is unavailable.
%==============================================================================%
Executing EntityGeneration Event (ev10) Time = 2.000000000000000
: Entity = <none> Priority = 300
: Block = Event-Based Entity Generator1
%..........................................................................%
Generating Entity (en3)
: Block = Event-Based Entity Generator1
The bottom entity generator generates an entity, which advances from block to block until it reaches the Attribute Scope block.
%==============================================================================%
Executing EntityGeneration Event (ev11) Time = 2.000000000000000
: Entity = <none> Priority = 310
: Block = Event-Based Entity Generator2
%..........................................................................%
Generating Entity (en4)
: Block = Event-Based Entity Generator2
%..........................................................................%
Entity Advancing (en4)
: From = Event-Based Entity Generator2
: To = Set Attribute2
%..........................................................................%
[...other messages...]
%..........................................................................%
Entity Advancing (en4)
: From = Get Attribute
: To = Attribute ScopeThe switch selects its IN1 entity input port. This causes the top entity generator to output the entity it just generated. This entity advances from block to block until it reaches the Attribute Scope block.
%==============================================================================%
Executing PortSelection Event (ev9) Time = 2.000000000000000
: Entity = <none> Priority = 4000
: Block = Input Switch
sedebug>>step over
%..........................................................................%
Entity Advancing (en3)
: From = Event-Based Entity Generator1
: To = Set Attribute1
%..........................................................................%
[...other messages...]
%..........................................................................%
Entity Advancing (en3)
: From = Get Attribute
: To = Attribute Scope
The plot of entities' Source attribute values shows that two entities from different entity generators depart from the switch every second.
Switch Departures When Entity Generations Are Processed First

[Source_attr.time, Source_attr.signals.values]
ans =
1 1
1 2
2 2
2 1
3 1
3 2
4 2
4 1
5 1
5 2
6 2
6 1
7 1
7 2
8 2
8 1
9 1
9 2
10 2
10 1Suppose the two entity generators and the switch have equal event priorities. By default, the application uses an arbitrary processing sequence for the entity-generation events and the port-selection events, which might or might not be appropriate in an application. To avoid bias by randomly determining the processing sequence for these events, set Execution order to Randomized in the model's Configuration Parameters dialog box.
Sample attribute values and the corresponding plot are below, but your results might vary depending on the specific random numbers.
Switch Departures When Processing Sequence is Random

[Source_attr.time, Source_attr.signals.values]
ans =
1 2
2 2
2 1
3 2
4 1
4 1
5 1
5 2
5 2
6 1
7 1
7 2
8 2
8 1
9 2
10 1
10 1
![]() | Assigning Event Priorities | Example: Effects of Specifying Event Priorities | ![]() |

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |