2026-11-23 (3): RTOS, Multithreading, Race Conditions#
Realtime Recap#
Consider work that must should committed within 1ms
Might take longer though ⟶ Glitch
Why is this possible?
Task (process/thread) might be preempted by scheduler in favor of another task
⟶ Deadline-miss due to preemption
#include <why-time.h>
#include <iostream>
#include <sys/mman.h>
#include <unistd.h>
int main()
{
if (geteuid() == 0) { // running as root
int error = mlockall(MCL_CURRENT|MCL_FUTURE); // defend address space against swapping
if (error == -1) {
perror("mlockall");
return 1;
}
}
const Why::TimeSpec one_ms(0, 1'000'000);
unsigned int num_rt_glitches = 0;
while (true) {
// perform work that fits in a 1ms (1000 writes should be ok)
Why::TimeSpec before = Why::now_monotonic();
{
for (uint64_t i=0; i<500'000; i++)
[[maybe_unused]] volatile int something = 42;
}
Why::TimeSpec after = Why::now_monotonic();
auto time_taken = after - before;
if (time_taken <= one_ms)
Why::sleep(one_ms - time_taken);
else
std::cout << "RT-OOPS #" << ++num_rt_glitches << " "
"(took " << time_taken.to_seconds()*1000.0 << " ms)" << std::endl;
}
return 0;
}
Start 100 CPU intensive processes ⟶ competitors for CPU time
$ for i in $(seq 100); do sha256sum /dev/zero& done
Run program as normal user: fair scheduling (priority 0)
$ ./2026-11-23-sched-hiccup ... RT-OOPS #510 (took 2.81667 ms) ...
⟶ relatively large oopses
Run program with realtime priority 1 (as root). 1 is the lowest of the realtime priorities.
$ sudo chrt -f 1 ./2026-11-23-sched-hiccup
⟶ better. Not perfect, because desktop Linux is bad at realtime.
Mother Of Race Conditions: Load/Modify/Store Conflict#
Polished Course Material (In Standard C++)#
Live-Coded Programs (In WhyOS Equivalents)#
Unprotected Conflict#
#include <why-thread.h>
#include <why-mutex.h>
#include <cstdint>
#include <iostream>
int main()
{
Why::init();
uint64_t poor_number = 0;
auto incrementer = [&poor_number](){
uint64_t n = 20'000'000;
while (n--)
poor_number++;
};
Why::Thread t1(incrementer);
Why::Thread t2(incrementer);
t1.join();
t2.join();
std::cout << poor_number << std::endl;
return 0;
}
Expected value of |
Actual value of |
|---|---|
40000000 |
Not 40000000 |
$ ./2026-11-23-load-modify-store
20818932
Protection Through Why::Mutex#
int main()
{
return 0;
}
Mutex Alternative For Simple Integer Types: std::atomic#
int main()
{
return 0;
}
Project: Door Commandline Loop - Enter Multithreading#
Two threads
Both inject events into Door FSM
⟶ Races to no end!
If even
i++is not thread safe, how can this be?void DoorFSM::dwim_button_pressed() { switch (_state) { ... case DOOR_OPEN: _motor.set_direction(MOTOR_LEFT); _state = DOOR_CLOSING; break; ... }
Realtime Operating Systems (RTOS) - WhyOS#
Why WhyOS?#
Why switch to WhyOS?
We want the door to be multithreaded, and create an architecture that is message based as many RTOS application are. WhyOS is a shim layer on top of Linux that looks much like an RTOS
Why message based?
FSM is shared by (currently 2, but generally many) threads ⟶ race conditions ahead!
Using Message Queues - “Message Based Architecture”#
#include <why-time.h>
#include <why-semaphore.h>
#include <why-messagequeue.h>
#include <why-thread.h>
#include <iostream>
int main()
{
Why::init();
Why::MessageQueue<int> queue(3);
Why::Thread producer([&queue](){
int i = 0;
while (true) {
queue.put(i++);
std::cout << "produced: " << elem << std::endl;
Why::sleep_milli(1000);
}
});
Why::Thread consumer([&queue](){
while (true) {
int elem = queue.get();
std::cout << "consumed: " << elem << std::endl;
}
});
producer.join();
consumer.join();
return 0;
}