Work in Progress
STM32 Microcontroller and Zephyr RTOS Development| Thread API Function | Description |
|---|---|
| k_thread_create() | Dynamically initializes and creates a Zephyr thread at runtime with a specified stack, entry function, priority, and startup delay |
| K_THREAD_DEFINE() | Statically defines a Zephyr thread at build time, with a specified stack size, entry function, priority, and startup delay |
| k_current_get() | Returns a pointer to the k_tid_t thread identifier of the currently executing Zephyr thread |
| k_thread_name_set() | Assigns a human-readable name to a Zephyr thread for easier debugging and identification |
| k_thread_name_get() | Returns the human-readable name assigned to a specified Zephyr thread |
| k_thread_priority_set() | Changes the priority of an existing Zephyr thread at runtime, affecting its scheduling precedence |
| k_thread_priority_get() | Returns the current scheduling priority of a specified Zephyr thread |
| k_thread_suspend() | Suspends a specified Zephyr thread, preventing it from executing until resumed with k_thread_resume() |
| k_thread_resume() | Resumes a previously suspended Zephyr thread, allowing it to execute again according to its scheduling priority |
| k_thread_abort() | Permanently terminates a specified Zephyr thread, preventing it from executing again |
| k_thread_join() | Blocks the calling thread until the specified Zephyr thread terminates or the timeout expires |
Many embedded applications require multiple tasks to operate independently, such as reading sensors, managing communication, and controlling outputs. Implementing all these operations within a single loop can make the software difficult to maintain and reduce system responsiveness. Threads address these challenges by enabling multiple tasks to execute concurrently, with the RTOS scheduler managing their execution based on scheduling policies and priorities.
A thread created with k_thread_create() is initialized explicitly at runtime. The application provides the thread control structure, stack, entry function, and priority. With K_NO_WAIT , the thread becomes ready to run immediately. This approach offers greater runtime control over thread creation and startup.
A thread declared with K_THREAD_DEFINE() is allocated statically and automatically initialized during system startup. This approach simplifies thread and stack management but offers less flexibility in controlling thread creation at runtime.
k_tid_t thread_id =
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
);
K_THREAD_DEFINE(name, stack_size, entry,
p1, p2, p3,
prio, options, delay); Zephyr schedules runnable threads based on their priorities, with lower numeric values indicating higher priority. Higher-priority threads generally preempt lower-priority threads, ensuring that time-critical tasks receive CPU attention first.

Preemptive scheduling: Suitable for most application threads. The scheduler allows a higher-priority thread to preempt a lower-priority thread as soon as it becomes ready. Preemptive threads use priority values of 0 or higher.
Cooperative scheduling: Suitable for threads requiring uninterrupted execution, such as critical state machines or protocol handlers. A cooperative thread runs until it voluntarily yields or blocks, without being preempted by other threads. Assign a negative priority value to enable this policy.
Time-slicing: Enables fair round-robin execution among equal-priority preemptive threads. Set CONFIG_TIMESLICING=y and configure the time slice using CONFIG_TIMESLICE_SIZE. Without time-slicing, equal-priority threads switch only when the running thread yields, blocks, or is otherwise rescheduled
Back to top of the page