Multithreading in RIOT OS Workshop
Multithreading in RIOT OS Workshop
Assigning the same priority to multiple threads can lead to scheduling uncertainty and difficulty in predicting CPU time allocation because they depend on cooperative scheduling and yielding by other threads with the same priority. To mitigate this, distinct priorities should be used whenever feasible, such that threads can preempt one another in a clear hierarchy .
Messaging and IPC facilitate structured communication between threads in RIOT OS, allowing for asynchronous and non-blocking operations when properly implemented. Developers should ensure message queues are initialized to avoid message loss and should be cautious with non-blocking message operations, using msg_try_send or msg_init_queue to manage potential spends adequately .
Creating threads within an ISR is discouraged because it could lead to unpredictable behavior and increased system complexity, potentially disrupting the real-time responsiveness and causing resource management issues. It's important to maintain clear separation between short-lived execution contexts like ISRs and more substantial processing handled by threads .
Tick-less scheduling in RIOT OS reduces power consumption by allowing the processor to sleep until a scheduled task is due, minimizing unnecessary wake-ups. This is particularly beneficial in embedded systems with power constraints, preserving battery life while potentially increasing system efficiency and performance by allocating CPU usage more judiciously .
The program should declare the threads with distinct priorities, taking care to avoid duplicate priorities. Each thread should perform its task, such as printing a specific message, based on a defined priority order. The scheduler will enforce this order, running higher priority threads before lower priority ones, ensuring that the output reflects the intended order of execution .
Optional flags during thread creation in RIOT OS modify how threads are scheduled and executed after their creation. Flags can control whether a thread starts immediately or whether it waits for a specific condition or event, thereby enabling complex behavior customization in thread execution flow .
To avoid data races when multiple threads manipulate a global variable, synchronization mechanisms like mutexes should be employed. For example, each thread could acquire a mutex before updating the shared variable and release it afterward, ensuring no two threads change the state of the variable simultaneously .
A tick-less OS, like that used in RIOT OS, wakes up only when necessary, such as when the next timer expires, rather than at regular fixed intervals as in a traditional tick-based system. This reduces unnecessary wake-ups, allowing the CPU to enter lower power states more often, potentially saving energy and improving efficiency .
A thread in RIOT OS can be STATUS_NOT_FOUND, STATUS_STOPPED, STATUS_SLEEPING, STATUS_MUTEX_BLOCKED, STATUS_RECEIVE_BLOCKED, STATUS_SEND_BLOCKED, STATUS_RUNNING, or STATUS_PENDING. Transitions are triggered by conditions like thread termination, sleeping due to lack of events, waiting on a mutex, message reception, or responding to a scheduling decision .
In RIOT OS's fixed priority scheduling, a thread waiting for a mutex enters a blocked state until the mutex becomes available. Higher priority threads will be scheduled over lower priority ones, even if they are blocked, ensuring efficiency and adherence to priority unless they are waiting for resources like a mutex .