Cooperative vs Preemptive Thread Priorities in Zephyr
Negative Zephyr thread priorities are cooperative and never preempted, while zero and above are preemptible. Learn the ranges, the rules and when to use each.

Zephyr uses one number to say two things about a thread: how urgent it is, and whether another thread may interrupt it. A priority below zero makes the thread cooperative, which means that nothing but itself can take the CPU away from it. A priority of zero or above makes it preemptible, which means a more urgent thread can take over at any moment. Getting this wrong produces systems that either freeze for no apparent reason or corrupt shared data at random. This post explains the two ranges in Zephyr 4.2, shows the effect with a runnable example, and gives a rule for choosing. It is a concept post that supports the guide to Zephyr threads and synchronization.
The numbering
Priorities are signed integers, and a smaller number is more urgent. Zephyr divides the range at zero:
| Priority | Class | Meaning |
|---|---|---|
-CONFIG_NUM_COOP_PRIORITIES to -1 | Cooperative | Runs until it blocks, sleeps or yields |
0 to CONFIG_NUM_PREEMPT_PRIORITIES - 1 | Preemptible | May be preempted by a more urgent thread |
| The idle thread | Preemptible, lowest | Runs when nothing else can |
The defaults are 16 cooperative and 15 preemptible priorities in many configurations, so cooperative priorities run from -16 to -1 and preemptible ones from 0 to 14, with the idle thread below them. Check CONFIG_NUM_COOP_PRIORITIES and CONFIG_NUM_PREEMPT_PRIORITIES in your build's .config, since they can differ. The macros K_PRIO_COOP(x) and K_PRIO_PREEMPT(x) express intent: K_PRIO_COOP(2) is -14 with 16 cooperative priorities, and K_PRIO_PREEMPT(3) is 3. The main thread uses CONFIG_MAIN_THREAD_PRIORITY, which defaults to 0, the most urgent preemptible priority. The reference is the scheduling section of the threads documentation.
Among all threads, the lowest number wins, so a cooperative thread, being negative, is more urgent than any preemptible thread.
What preemption means
On a preemptible thread, the scheduler runs after every event that could make a more urgent thread ready: a timer expiring, an interrupt giving a semaphore, another thread posting to a queue. If the newly ready thread has a smaller priority number than the current one, the current thread stops, wherever it is, and the new one runs. The first thread resumes later with its registers restored, and it never knows it was interrupted. That is why shared data needs protection: the switch can happen between any two instructions.
On a cooperative thread, the scheduler still runs after those events, but it does not switch away from a cooperative thread because of a more urgent ready thread. The cooperative thread keeps the CPU until it calls a function that blocks, such as k_msleep, k_sem_take with a wait, or k_yield. Interrupt handlers still run, because they are not threads, and they can make other threads ready, but those threads wait.
A demonstration
Two threads share a counter, each incrementing it 100,000 times without a lock. The first run uses preemptible priorities and the second cooperative ones. The code is the same except for the priority:
#include <zephyr/kernel.h>
#define N 100000
static volatile int counter;
static void worker(void *p1, void *p2, void *p3)
{
for (int i = 0; i < N; i++) {
int tmp = counter;
tmp = tmp + 1;
counter = tmp;
}
}
#ifdef CONFIG_APP_COOP
#define WORKER_PRIO K_PRIO_COOP(5)
#else
#define WORKER_PRIO K_PRIO_PREEMPT(5)
#endif
K_THREAD_DEFINE(w1, 1024, worker, NULL, NULL, NULL, WORKER_PRIO, 0, 0);
K_THREAD_DEFINE(w2, 1024, worker, NULL, NULL, NULL, WORKER_PRIO, 0, 0);
int main(void)
{
k_msleep(1000);
printk("counter = %d (expected %d)\n", counter, 2 * N);
return 0;
}The Kconfig file defines the switch, and prj.conf turns on time slicing, which lets equal-priority preemptible threads interleave:
mainmenu "Priority demo"
config APP_COOP
bool "Use cooperative priorities"
source "Kconfig.zephyr"CONFIG_PRINTK=y
CONFIG_TIMESLICING=y
CONFIG_TIMESLICE_SIZE=1
CONFIG_TIMESLICE_PRIORITY=0Built with preemptible priorities, the two threads are sliced every millisecond, and the read-modify-write sequence is interrupted partway, so updates are lost. The result is below the expected value, and it varies from run to run:
*** Booting Zephyr OS build v4.2.0 ***
counter = 131846 (expected 200000)The number shown is an example. Built with -DCONFIG_APP_COOP=y, the threads are cooperative, time slicing does not apply to them, and the first thread runs its whole loop before the second starts:
*** Booting Zephyr OS build v4.2.0 ***
counter = 200000 (expected 200000)The cooperative result is correct here only because each thread's loop never blocks, so nothing can interleave them. That is an accident of this code, and it breaks as soon as a worker calls a function that sleeps or waits, and it is fragile to maintain. Treat cooperative scheduling as a tool for short, known stretches, and not as a substitute for a lock.
When to use cooperative threads
Cooperative priorities have a few good uses.
Short, uninterruptible sequences. A routine that must complete without another thread touching a data structure, and that takes microseconds, can run in a cooperative thread. It is simpler than a lock when no interrupt touches the data.
Driver internals. Some Zephyr subsystems use cooperative priorities for threads that serialize access to hardware, such as parts of the Bluetooth controller. The system work queue can also be cooperative, depending on CONFIG_SYSTEM_WORKQUEUE_PRIORITY, and you should check the value in your build before assuming.
Handlers that must run to completion. Work that is meaningless if cut in half, and that is bounded, fits a cooperative thread.
The danger is that a cooperative thread that loops without blocking freezes every thread of lower urgency, forever, including all preemptible ones. A bug in a cooperative thread is a system hang.
When to use preemptible threads
Use preemptible priorities for everything else, which is nearly all application code. They respond to urgent events within a bounded time, because a more urgent thread can run the moment it becomes ready, and a misbehaving thread cannot freeze the whole system unless it is at a very urgent priority. Pair them with synchronization primitives for shared data.
Inside the preemptible range, assign numbers by deadline: the thread with the shortest deadline gets the smallest number. Leave gaps, such as 2, 5, 8, 11, so you can insert threads later.
Protecting a section within a preemptible thread
When a preemptible thread needs a short stretch without interruption from other threads, k_sched_lock() and k_sched_unlock() make it temporarily non-preemptible:
k_sched_lock();
update_two_related_fields();
k_sched_unlock();This does not stop interrupts, so an ISR can still change the data. Do not block inside the locked region, and keep it very short. For protection against other threads and interrupts, use a mutex or a spinlock, as the guide explains. Changing the priority of a thread at run time with k_thread_priority_set also moves it between classes, for example to make a thread cooperative while it does something delicate, then restore it. That pattern is easy to get wrong, so prefer the lock calls.
How time slicing fits in
With CONFIG_TIMESLICING=y, preemptible threads at the same priority share the CPU in slices of CONFIG_TIMESLICE_SIZE milliseconds. Cooperative threads are not sliced. A cooperative thread and a preemptible one never share a slice, because the cooperative one has the more urgent priority and runs first.
Without time slicing, equal-priority preemptible threads switch only when one blocks. This makes them behave cooperatively with respect to each other, though a more urgent thread can still preempt them. Many small systems leave slicing off and rely on threads blocking often.
Rules of thumb
Make application threads preemptible, in the range 5 to 12.
Use cooperative priorities only for short, bounded work that must not be interrupted by threads.
Never put a thread that waits on something slow at a cooperative priority, since the CPU is held while it waits only if it spins, but any bug that spins freezes everything.
Check the system work queue's priority and the priorities of any stacks you use, such as networking and Bluetooth, before choosing yours.
Do not rely on cooperative scheduling as protection for shared data when an interrupt or a more urgent thread can touch it.


