Zephyr RTOS: How to create threads
The Zephyr RTOS has basically two methods to create a thread.
- Using the kernel API
Just like the other RTOS such as FreeRTOS, we can use the kernel API to create a thread.
k_thread_create(struct k_thread *new_thread,
Detailed documentation of this API can be found here: https://docs.zephyrproject.org/latest/reference/kernel/threads/index.html#c.k_thread_create
The most interesting parameter to me is the delay option. The new thread can be scheduled immediately or for a delayed start.
- delay — Scheduling delay, or K_NO_WAIT (for no delay).
When tasks are created from the application task (e.g.init_task), it is common to assign the init task with a higher priority than the other tasks to be created in the init task to avoid preemption during initialization.
2. Using K_THREAD_DEFINE macro
K_THREAD_DEFINE(thread_id, STACKSIZE, thread_entry_point, NULL, NULL, NULL, PRIORITY, 0, 0);
This might look unfamiliar but others like uItron or OSEK have a similar concept of static creation of Task at compile time.