No ACL setup yet! Denying access to everyone.
en:edu:zmitac:s2:aih:lab:nrf52threads

This is an old revision of the document!


Zephyr OS Threads

In this laboratory, we will learn thread management and switching in Zephyr OS. The thread is a sequential stream of instructions. Thread can be the process itself or more threads can form the process. In a computer system with a single-core processor currently executed thread occupies all processor resources. Multi-threading operation system run on such processors uses time-sharing of resources to execute many threads simultaneously. Of course simultaneously in this situation means that threads are executed one-by-one.

Thread switching methods

Switching between threads is made by the part of the operating system known as a scheduler. The scheduler decides which thread should be executed next. To do so it considers the priorities of the threads and history of execution. There are two general methods of switching between threads that should be executed in parallel:

  • cooperative
  • preemptive

In the cooperative method, the thread is executed until it gives the control back to OS. Thus thread can starve others while not releasing resources. This method is often used in embedded systems due to its low complexity and system overhead. Cooperative threads in Zephyr have a negative priority value (thus they have a relatively higher priority). In the preemptive method, the thread is automatically suspended by the operating system when it decides to do so. Usually, the thread is given some period and is interrupted at the end of this period if there exists another thread to be executed. This method is implemented in general-purpose operating systems like Windows or Linux. It is much safer but more complicated and resource-consuming. In Zephyr preemptive threads have a positive priority number (thus they have a relatively lower priority). Zephyr OS implements both methods of thread switching.

Cooperative thread switching

A cooperative thread gives control to the operating system in a few situations:

  • if it decides to terminate by returning from its main function or by calling the k_thread_abort().
  • if it decides to yield by calling the function k_yield().

While implementing the thread with the infinite loop it is recommended to call the k_yield() function every pass of the loop.

en/edu/zmitac/s2/aih/lab/nrf52threads.1700743803.txt.gz · Last modified: by ktokarz