Zephyr RTOS: How to create threads

Jaehee Kwon
1 min readMar 28, 2022

The Zephyr RTOS has basically two methods to create a thread.

  1. Using the kernel API

Just like the other RTOS such as FreeRTOS, we can use the kernel API to create a thread.

__syscall k_tid_t 
k_thread_create(struct k_thread *new_thread,
k_thread_stack_t *stack,
size_t stack_size,
k_thread_entry_t entry,
void *p1,
void *p2,
void *p3,
int prio,
uint32_t options,
k_timeout_t delay);

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.

--

--