CodeBlanks Logo Icon CodeBlanks

Real-time

Learn real-time C++ and timing constraints.

Real-time

A real-time system requires operations to complete within a specific deadline. The key is not speed but determinism: how long each task takes must be known in advance.

There are two kinds of real-time: hard real-time (missing a deadline fails the system — airbag control, for example) and soft real-time (missing a deadline degrades quality but the system still runs — video playback, for example). Embedded systems usually carry real-time constraints.

Determinism

In real-time systems determinism is being able to know a task's duration in advance. Operations with uncertain duration like dynamic memory allocation, cache misses, garbage collection are avoided. The task duration is computed for the worst case.

Example: an embedded loop is while (true) { runTask(); }. Each runTask call's duration must be known in advance; otherwise the deadline may be missed. jitter (time variability) is kept small; this means the task duration is the same each time.

Practice Write a real-time loop

Define void runTask() (an empty body is fine). In main define int count = 0;, write for (int i = 0; i < 3; ++i) { runTask(); count++; } and write std::cout << count;.

Starter code

main.cpp
#include <iostream>

// Define the runTask function here

int main() {
    // Write a loop calling runTask three times and print count
    return 0;
}

Sample solution

main.cpp
#include <iostream>

void runTask() {
}

int main() {
    int count = 0;
    for (int i = 0; i < 3; ++i) {
        runTask();
        count++;
    }
    std::cout << count;
    return 0;
}

Expected output

3

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Complete the loop comment

Question
for (;;) { /* ____ loop */ runTask(); }
Answer
for (;;) { /* real-time loop */ runTask(); }

Explanation: The infinite loop is the skeleton of a real-time system.

2 Match each term to its role

Term Meaning
hard real-time Deadline critical
soft real-time Deadline flexible
determinism Fixed time upper bound
jitter Time variability

3 What does hard real-time mean?

main.cpp
// airbag control
  • Deadline is flexible
  • Always the fastest
  • Deadline is critical, missing it fails the system Correct answer
  • Requires an operating system

Explanation: In hard real-time, missing a deadline counts as system failure.

All lessons in Expert