Definition
A Zephyr thread is the unit of execution that the Zephyr §6.33 kernel schedules: one sequence of instructions with its own stack (the memory that holds its local variables and function calls), a thread control block (the kernel’s record of the thread), an entry function where it starts, and a priority. All threads share one address space, and without Zephyr’s optional user mode every thread can access all memory. A thread ends when its entry function returns or when it is aborted, and ending releases nothing the thread held.
The idea
When a process exits on a desktop operating system, the operating system closes its files, frees its memory and releases its locks, because it tracked everything the process owned. A Zephyr thread is not a process. The kernel tracks the thread’s state and its place in the queues of waiting threads, but not the memory, mutexes §9.5, buffers or device transfers that the thread acquired. When the thread ends, all of those stay exactly as they were.
Two lifetimes must therefore be kept apart: the life of the thread, which the kernel manages, and the lifetime of each resource the thread used, which the application manages. A thread that stops can leave behind a locked mutex that nobody will unlock, a heap block that nobody will free, or a buffer that a device is still reading. For every resource, the application must plan who releases it and when.
How it works
Creation. A thread is created with k_thread_create() at run time or with K_THREAD_DEFINE at build time. Its properties are:
a stack area, which the application reserves, usually as a static array §14.5 of fixed size;
a thread control block, a
struct k_threadin which the kernel keeps the thread’s saved registers and state;an entry function, and up to three arguments for it;
a priority: a negative value makes the thread cooperative, meaning no other thread takes the processor from it until it waits or yields; zero or a positive value makes it preemptive, meaning a higher-priority thread can take over at once; a lower number means a higher priority;
options, such as
K_ESSENTIAL, which makes the end of the thread a fatal error of the whole system, orK_USER, which starts the thread in user mode;a start delay.
States. A thread is ready when nothing prevents it from running, and the scheduler runs the highest-priority ready thread. A thread is waiting (Zephyr’s documentation also says pending) while it waits for a kernel object, such as a semaphore §9.6 that is not available, or for a timeout §9.34. A thread can also be suspended by k_thread_suspend(), or not yet started. Once it has ended, it is dead.
Ending. A thread ends in one of three ways.
It terminates by returning from its entry function. Zephyr’s documentation makes the thread responsible for releasing shared resources, such as mutexes and dynamically allocated memory, before it returns, because the kernel does not reclaim them.
It is aborted by
k_thread_abort(), called by another thread or by itself. The thread stops at whatever instruction it had reached. The kernel removes it from the ready queue, the timeout queue and any queue of threads waiting for a kernel object, and releases nothing else; the kernel’s documentation adds that with priority inheritance §9.17 enabled, the record of mutexes the thread held is left inconsistent.It suffers a fatal error, such as a memory access fault. Zephyr’s default fatal error handler then halts or resets the whole system. If the application installs its own handler and that handler returns, the kernel aborts only the faulting thread. Ending an essential thread is itself a fatal system error.
Confirming the end. Another thread waits for a thread to end with k_thread_join(thread, timeout), which sleeps until the target has ended or the timeout expires, and returns 0 only if the target has ended. When k_thread_abort() is called on another thread, it returns only once the target is guaranteed never to run again. After either confirmation, the kernel makes no further use of the target’s thread control block, and the application may reuse that memory and the stack, for example to start a new thread. A flag that the thread sets just before returning is not such a confirmation: another thread can see the flag before the kernel has finished ending the thread.
A planned stop. Aborting stops a thread at an arbitrary instruction, so Zephyr’s documentation prefers asking a thread to end itself. The controlling thread sets a stop request, for example by giving a semaphore that the worker checks. The worker reaches a point where it can release everything it holds, releases it, and returns. The controlling thread then calls k_thread_join().
Worked example
A worker thread T handles requests. For each request it allocates a 64-byte buffer from a 2048-byte heap, locks a mutex M that protects a shared table, and starts a transfer in which a peripheral reads the buffer by direct memory access §4.36 (DMA), that is, without the processor. T then waits for the transfer to finish. A monitoring thread U decides that T is stuck and calls k_thread_abort(T) while T waits.
After the abort:
The 64 bytes stay allocated, and the only pointer to them was in T’s dead stack. If this happens on every request, then after aborted requests the heap has no room for another buffer, and in practice the heap runs out sooner, because the allocator keeps its own bookkeeping inside the heap.
Mutex M stays locked, with the dead T recorded as its owner. The next thread that calls
k_mutex_lock(&M, K_FOREVER)waits forever.The peripheral keeps reading the buffer. If U now frees the buffer and the allocator hands the same memory to another request, the peripheral reads that request’s data. This is a use-after-free §4.6 that no software check sees.
A correct shutdown runs in this order. U sets a stop request. T, at its next check, cancels the transfer and waits until the peripheral confirms that it no longer uses the buffer. T frees the buffer, unlocks M and returns. U calls k_thread_join(&T, K_MSEC(100)). A return value of 0 means T has ended, and its thread control block and stack may be reused. Any other value means T has not ended within 100 ms. U must then not reuse T’s stack, and must escalate, for example by resetting the device, because aborting T now would bring back every problem listed above.
Stated precisely
For a thread , three conditions must be kept apart:
no longer runs, and never will again;
the kernel no longer uses ’s thread control block or stack;
no resource that acquired has any remaining user, and each such resource has been released exactly once.
Returning from the entry function, or being aborted, brings about the first condition. A return value of 0 from k_thread_join(), or the return of k_thread_abort() called on a thread other than the caller, establishes the second condition for the caller. Only the application’s own shutdown protocol establishes the third: the kernel never unlocks a mutex, gives a semaphore, frees heap memory or stops a device transfer on behalf of a thread that has ended.
Limits and common mistakes
Zephyr threads are not processes. Without user mode, all threads share all memory, so a fault in one thread can corrupt any other. With user mode, isolation covers memory and kernel objects, not the resources that the application itself hands out.
Aborting is a last resort. The thread stops between any two instructions, possibly halfway through an update of a shared structure, which then stays half updated.
A finite timeout does not repair a mutex whose owner has ended. k_mutex_lock() with a timeout returns an error instead of waiting forever, but the mutex stays locked, and every later locker fails the same way.
Zephyr treats the main thread as essential while it runs main(), so ending it then is a fatal error of the whole system.
The stack area and thread control block belong to the application, which may free or reuse them only after the end has been confirmed.
Questions answered
Who releases resources when a thread exits or is aborted?
The application releases a thread’s resources: either the code of the thread itself before it ends, or the code that coordinates the thread’s shutdown. Zephyr’s kernel reclaims nothing that a thread acquired. When a thread returns from its entry function or is aborted, mutexes it holds stay locked, heap memory it allocated stays allocated, and device transfers it started keep running. The kernel only removes the thread from scheduling and from the queues of threads waiting for kernel objects, and, once the end is complete, stops using the thread’s control block and stack.
Every resource therefore needs a stated owner and a release rule. The usual pattern is a planned stop: ask the thread to end; let it cancel its outstanding device work, wait for the device to confirm, free its memory, unlock its mutexes and return; then confirm the end with k_thread_join() before reusing the thread’s control block or stack. Aborting a thread is a last resort, because it stops the thread at an arbitrary instruction and leaves all of these resources behind.
Related entries: Asynchronous completion (§9.28), Cancellation (§9.29), Ownership (§4.2), Mutex (§9.5), Use-after-free (§4.6), Cooperative and preemptive scheduling (§6.37), Zephyr RTOS (§6.33), Priority inheritance (§9.17). Further reading: [13, 83].