Execution Control Patterns
Learning Objectives
This tutorial explores four common patterns for controlling execution flow, each with different trade-offs:
- Busy looping for minimal latency, at the cost of a saturated CPU core
- Timer-driven wake-ups for periodic work, at the cost of responsiveness between ticks
- Event-driven wake-ups for responsiveness and efficient CPU usage, at the cost of OS wake-up latency
- Multiplexing multiple approaches in one thread
A pattern often seen in middleware solutions is to couple data flow with control flow. Threads are put to sleep when no data is available to process, and wake-up signals are emitted automatically when data arrives.
This coupling forces a wake-up per sample. Workloads whose useful unit of work spans multiple samples pay the cost of system calls and context switches on wake-ups that produce no work. Examples include sensor fusion algorithms and algorithms that operate over a window of data.
iceoryx2 instead decouples control flow from data flow. The user decides when a participant thread is put to sleep, when it is made ready to run, and what signals trigger the transition.
The following sections cover the patterns for managing application execution with iceoryx2.
Busy looping
Busy looping continuously polls ports for new data without sleeping the thread. This approach minimizes latency by eliminating context switches and system calls, making it ideal for ultra-low-latency scenarios where efficient CPU usage is not a priority.
A busy loop saturates one CPU core for as long as it runs, so it fits hot-path consumers on isolated cores, such as high-frequency trading and kernel-bypass networking, where the latency budget justifies dedicating the core. Practical deployments pin the polling thread to a specific core and raise its scheduling priority.
Example: Market Data Consumer
A trading process that consumes a stream of price ticks from a market data feed has no fixed schedule. Ticks arrive whenever the market sends them, and the consumer must react with the smallest possible delay.
const
TARGET_PRICE = 100;
type
TPriceTick = record
public
InstrumentId: UInt32;
Price: Double;
end;
var Node := TNodeBuilder.New
.Name(TNodeName.Create('MarketDataConsumer').Value)
.Create(TServiceType.Ipc)
.Value;
var TicksService := Node
.ServiceBuilder(TServiceName.Create('market/ticks').Value)
.PublishSubscribe<TPriceTick>
.OpenOrCreate
.Value;
var OrdersService := Node
.ServiceBuilder(TServiceName.Create('market/orders').Value)
.PublishSubscribe<UInt32>
.OpenOrCreate
.Value;
var Subscriber := TicksService
.SubscriberBuilder
.Create
.Value;
var OrdersPublisher := OrdersService
.PublisherBuilder
.Create
.Value;
while (Node.Wait(TDuration.Zero).IsOk) do
begin
var Tick := Subscriber.Receive.Value;
while (Tick <> nil) do
begin
if (Tick.Payload.Price > TARGET_PRICE) then
OrdersPublisher.SendCopy(Tick.Payload.InstrumentId);
Tick := Subscriber.Receive.Value;
end;
end;
The outer Node.Wait(TDuration.Zero) returns immediately and only errors on SIGTERM/SIGINT, keeping the loop tight while still allowing a clean shutdown. Each queued tick is drained inline and checked against the target price before the next poll, so the order sees only the busy-loop's own polling overhead, with no OS wake-up. Thread pinning and scheduling priority are configured outside iceoryx2, typically at thread-spawn time.
Timer-driven
Timer-driven execution puts threads to sleep after a unit of work is done and reschedules them on a regular interval.
This approach limits the overhead from context switches and system calls to the timer events. Keep in mind that the precision of timer events is governed by the platform on which iceoryx2 is deployed.
This pattern is well-suited to tasks that have a well-defined schedule, such as heartbeat mechanisms or time-triggered architectures.
Example: Cruise Control
This cruise control application reads the vehicle's current speed, computes a throttle command, and sends it to the actuator at a fixed rate. The control math depends on a stable loop period.
const
SETPOINT_MKH = 100;
KP = 0.1;
var ControlPeriod := TDuration.FromMillis(20);
var Node := TNodeBuilder.New
.Name(TNodeName.Create('CruiseController').Value)
.Create(TServiceType.Ipc)
.Value;
var SpeedService := Node
.ServiceBuilder(TServiceName.Create('vehicle/speed').Value)
.PublishSubscribe<Single>
.SubscriberMaxBufferSize(1)
.OpenOrCreate
.Value;
var ThrottleService := Node
.ServiceBuilder(TServiceName.Create('vehicle/throttle').Value)
.PublishSubscribe<Single>
.OpenOrCreate
.Value;
var SpeedSubscriber := SpeedService
.SubscriberBuilder
.Create
.Value;
var ThrottlePublisher := ThrottleService
.PublisherBuilder
.Create
.Value;
var CurrentSpeed: Single := 0;
while (Node.Wait(ControlPeriod).IsOk) do
begin
var Sample := Subscriber.Receive;
if (Sample.IsOk) then
begin
var SampleValue := Sample.Value;
if (SampleValue = nil) then
// no new sample — reuse last known speed
else
CurrentSPeed := SampleValue.Payload^;
end
else
begin
// Handle receive failure
end;
var Throttle: Single := EnsureRange(KP * (SETPOINT_KMH - CurrentSpeed), 0.0, 1.0);
ThrottlePublisher.SendCopy(Throttle);
end;
Node.wait(ControlPeriod) paces the loop; nothing else triggers a tick. The speed service is configured with SubscriberMaxBufferSize(1), so the queue only ever holds the latest reading; a single Receive() per tick fetches it. A throttle command goes out unconditionally. If no new speed sample arrived, the controller reuses the last known value.
Event-driven
Like timer-driven execution, event-driven execution puts threads to sleep after a unit of work is done. The difference is that threads are woken up in response to notifications via the event messaging pattern. Events are defined by the user and can be triggered from anywhere in the system.
Notifiers and listeners are many-to-many on a service, so any notifier can trigger any listener attached to that service. Additionally, the TEventId, included with every notification, can be used to disambiguate notifications on a single service.
This approach combines efficient CPU usage with timely response when work arrives: threads wake only when there is work to do.
Events carry no payload beyond the TEventId, so to move data alongside a wake-up, an event service must be paired with a publish-subscribe service: the producer publishes a sample, then notifies; the consumer wakes and drains available samples. This approach reconstructs the data-plus-wake-up flow that other middlewares couple implicitly.
Example: Event Data Recorder
A vehicle event-data recorder packages and uploads sensor data to the cloud whenever an incident occurs (for instance, when the emergency brake fires) so the scenario can be replayed for analysis. Sensors publish continuously on their own schedules; the recorder only has work to do at incident time.
The event-data recorder does not need to wake on publish of every sample. This would introduce overhead when no real work needs to be done. Instead, a single event service signals when the work should be done.
type
TWheelSpeed = record
public
TimestampNS: UInt64;
Axle: UInt32;
SpeedMps: Single;
end;
type
TImuSample = record
public
TimestampNS: UInt64;
AccelMps2: Single;
YawRateRps: Single;
end;
type
TObjectTrack = record
public
TimestampNS: UInt64;
DistanceM: Single;
RelativeSpeedMps: Single;
end;
const
LOOKBACK_WINDOW = 256;
var Node := TNodeBuilder.New
.Name(TNodeName.Create('EventDataRecorder').Value)
.Create(TServiceType.Ipc)
.Value;
var WheelSpeedService := Node
.ServiceBuilder(TServiceName.Create('vehicle/wheel_speed').Value)
.PublishSubscribe<TWheelSpeed>
.SubscriberMaxBufferSize(LOOKBACK_WINDOW)
.OpenOrCreate
.Value;
var ImuService := Node
.ServiceBuilder(TServiceName.Create('vehicle/imu').Value)
.PublishSubscribe<TImuSample>
.SubscriberMaxBufferSize(LOOKBACK_WINDOW)
.OpenOrCreate
.Value;
var ObjectTrackService := Node
.ServiceBuilder(TServiceName.Create('vehicle/object_track').Value)
.PublishSubscribe<TObjectTrack>
.SubscriberMaxBufferSize(LOOKBACK_WINDOW)
.OpenOrCreate
.Value;
var TriggerService := Node
.ServiceBuilder(TServiceName.Create('vehicle/incident_trigger').Value)
.Event
.OpenOrCreate
.Value;
var WheelSpeed := WheelSpeedService
.SubscriberBuilder.Create.Value;
var Imu := ImuService
.SubscriberBuilder.Create.Value;
var ObjectTrack := ObjectTrackService
.SubscriberBuilder.Create.Value;
var Trigger := TriggerService
.ListenerBuilder.Create.Value;
while (Node.Wait(TDuration.Zero).IsOk) do
begin
Trigger.TimedWait(
procedure(const AEvent: TEventActivation)
begin
while (True) do
begin
var Sample := WheelSpeed.Receive.Value;
if (Sample = nil) then
Break;
// append to upload bundle
end;
while (True) do
begin
var Sample := Imu.Receive.Value;
if (Sample = nil) then
Break;
// append to upload bundle
end;
while (True) do
begin
var Sample := ObjectTrack.Receive.Value;
if (Sample = nil) then
Break;
// append to upload bundle
end;
// ship the bundle upstream for offline scenario analysis
end, TDuration.FromSecs(1)).Value;
end;
Each pubsub service is configured with a large SubscriberMaxBufferSize, so the recorder accumulates a lookback window without dropping samples between triggers. A single TimedWait blocks for the trigger and drains any further triggers queued behind it, so several sensors signalling the same incident produce one upload rather than several near-empty ones. Draining each subscriber to exhaustion then captures the full pre-event window. The one-second timeout on TimedWait ensures the outer Node.wait(TDuration.Zero) runs at least once per second, so the recorder can notice SIGTERM/SIGINT and shut down cleanly even when no triggers are arriving.
Multiplexing
Multiplexing combines several of the patterns above into a single thread. iceoryx2 provides a WaitSet that holds a set of attachments: interval timers, event notifications, and deadline-monitored event sources. The execution of the thread can be handed over to the WaitSet, which puts the thread to sleep and wakes it up when any attachment is triggered. When the wake-up arrives, the WaitSet invokes a user-supplied callback identifying which attachment triggered the wake-up.
A TWaitSet supports three kinds of attachments:
AttachInterval(Duration)— a periodic tick, equivalent to the timer-driven pattern but composable with other sources.AttachNotification(Listener)— wake on any event from the attached listener, equivalent to the event-driven pattern.AttachDeadline(Listener, Duration)— wake on an event or if no event arrives within the deadline. The deadline resets every time an event is received e.g. fail safe if no sensor sample arrives within 100 ms.
Multiplexing keeps the efficient sleep/wake behaviour of the single-source patterns while consolidating several concerns into one thread at the cost of bookkeeping complexity, the callback has to disambiguate which source fired before handling it.
Example: Vehicle supervisor
A vehicle supervisor pulls together three concerns into one thread: a periodic control step, a sensor whose silence is itself a signal, and an emergency-stop input that can interrupt either of them. Multiplexing these on a single WaitSet lets the supervisor sleep between events and react to whichever one fires next.
var ControlPeriod := TDuration.FromMillis(20); // 50 Hz
var SpeedDeadline := TDuration.FromMillis(100);
var Node := TNodeBuilder.New
.Name(TNodeName.Create('VehicleSupervisor').Value)
.Create(TServiceType.Ipc)
.Value;
var SpeedSignal := Node
.ServiceBuilder(TServiceName.Create('vehicle/speed').Value)
.Event
.OpenOrCreate
.Value;
var EmergencyStopSignal := Node
.ServiceBuilder(TServiceName.Create('vehicle/emergency_stop').Value)
.Event
.OpenOrCreate
.Value;
var SpeedListener := SpeedSignal
.ListenerBuilder.Create.Value;
var EmergencyStopListener := EmergencyStopSignal
.ListenerBuilder.Create.Value;
var WaitSet := TWaitSetBuilder.New
.Create(TServiceType.Ipc)
.Value;
var TickGuard := WaitSet
.AttachInterval(ControlPeriod);
var SpeedGuard := WaitSet
.AttachDeadline(SpeedListener, SpeedDeadline);
var EmergencyStopGuard := WaitSet
.AttachNotification(EmergencyStopListener);
WaitSet.WaitAndProcess(
function(const AAttachmentId: TAttachmentId): TCallbackProgression
begin
if (AAttachmentId.HasEventFrom(TickGuard)) then
begin
// run periodic control step
end;
if (AAttachmentId.HasEventFrom(SpeedGuard)) then
begin
SpeedListener.TryWait(
procedure(const AEvent: TEventActivation)
begin
// drain
end).
// process new speed reading
end;
if (AAttachmentId.HasEventFrom(EmergencyStopGuard)) then
begin
EmergencyStopListener.TryWait(
procedure(const AEvent: TEventActivation)
begin
// drain
end).
// engage emergency stop
end;
Result := TCallbackProgression.Continue;
end);
{ Release guards *before* other objects}
EmergencyStopGuard.Release;
SpeedGuard.Release;
TickGuard.Release;
Each attachment type maps to one of the patterns covered earlier: AttachInterval is the control-step heartbeat, AttachNotification is the emergency-stop wake-up, and AttachDeadline combines both. The supervisor receives ordinary speed updates as notifications, and the deadline fires only if the speed sensor goes silent past the budget. Each notification branch must drain its listener with TryWait before returning. While unread notifications remain, the TWaitSet treats the source as still ready and re-enters the callback immediately, so a branch that doesn't drain spins the thread.
Further Reading
-
Understand the Event messaging pattern
Understand the
Eventmessaging pattern. -
Understand the
TWaitSetcomponent.