OS Primitives: Threads#

Threads Basics#

  • Scheduled by the OS: timeslices

  • Zephyr: cooperative vs. preemptive threads

  • One stack per thread

  • Threads may block on OS primitives like a mutex or message queue.

  • Priorities: when a higher priority thread becomes runnable, the currently running thread is suspended, and a context switch is made to run the higher priority thread

Main Thread#

  • One thread is always there, implicitly created

  • ⟶ Main thread

  • Zephyr: runs at highest priority

  • ⟶ Careful

#include <why-thread.h>
#include <print>

int main()
{
    Why::init();

    auto mainthread = Why::Thread::self();
    std::println("priority: {}/{}", (int)mainthread.priority(), mainthread.prioritystr());
    return 0;
}

jjj Handler Threads#