Preemptive vs Cooperative Scheduling in Embedded Systems
Preemptive schedulers take the CPU away on their own, cooperative ones wait for a task to yield. Compare latency, safety and code cost with FreeRTOS examples.

Two kernels can run the same tasks and behave very differently, depending on a single question: who decides when a running task stops? In a preemptive scheduler the kernel decides, by interrupting the task. In a cooperative scheduler the task decides, by calling a yield function or blocking. That choice sets your worst-case latency, how much locking you need and how your bugs look. This post compares the two with FreeRTOS Kernel V11.3.3, which supports both through one configuration switch, and shows what each does to a pair of identical tasks. The cornerstone guide on RTOS fundamentals for microcontrollers sets the vocabulary this post uses.
The definitions
A preemptive scheduler can remove the running task from the CPU at any scheduling point, whether or not the task agrees. Scheduling points include the tick interrupt, an interrupt that unblocks a higher-priority task, and any kernel call that makes a higher-priority task ready. The highest-priority ready task always runs.
A cooperative scheduler switches only when the running task gives up the CPU: by calling a yield, by blocking on a queue or delay, or by returning from its function. Interrupts still run, but they never cause a task switch on return. A task that loops without yielding stops the whole system.
FreeRTOS selects the mode with configUSE_PREEMPTION. Set it to 1 for preemptive scheduling and 0 for cooperative:
#define configUSE_PREEMPTION 1 /* 0 selects cooperative scheduling */
#define configUSE_TIME_SLICING 1 /* round-robin between equal priorities */
#define configTICK_RATE_HZ ( 1000 )Watching the difference
Two tasks at the same priority each print three lines, then busy-wait to simulate work. Here is the code:
#include <stdio.h>
#include "FreeRTOS.h"
#include "task.h"
static void vWorker(void *pvParameters)
{
const char *pcName = (const char *) pvParameters;
for (;;)
{
for (int i = 1; i <= 3; i++)
{
printf("%s%d\n", pcName, i);
for (volatile uint32_t d = 0; d < 400000; d++) { }
}
taskYIELD();
}
}
int main(void)
{
xTaskCreate(vWorker, "A", 256, "A", 1, NULL);
xTaskCreate(vWorker, "B", 256, "B", 1, NULL);
vTaskStartScheduler();
for (;;) { }
}With configUSE_PREEMPTION set to 0, each task runs until it calls taskYIELD(), so the output arrives in clean blocks:
A1
A2
A3
B1
B2
B3
A1
A2
A3With preemption on and time slicing enabled, the kernel cuts in at each tick. The delay loop takes longer than one tick on a typical clock, so the lines interleave, and the exact pattern depends on timing:
A1
B1
A2
B2
A3
B3The second output is illustrative; the point is that the order is no longer under the tasks' control. That has a direct cost: any data the two tasks share can now change between two instructions of either one.
What changes in the design
| Concern | Preemptive | Cooperative |
|---|---|---|
| Worst-case response to an event | Time to the next scheduling point, usually the interrupt return | Longest stretch any task runs without yielding |
| Shared data | Needs protection: mutex, critical section, queue | Safe between yield points |
| Priority inversion | Possible; needs priority inheritance | Cannot happen the same way |
| Runaway task | Higher-priority tasks still run | Freezes everything at that priority and below, and often the whole system |
| Code style | Tasks can be written as simple loops | Tasks must yield or block at the right places |
| Stack use | One stack per task | One stack per task in an RTOS; one total in a run-to-completion scheduler |
Latency is the argument for preemption. In a cooperative system, a 50 ms computation in one task delays an urgent event by up to 50 ms, even if a high-priority task is waiting for it. In a preemptive system, the urgent task runs at the next interrupt return.
Safety from races is the argument for cooperation. A cooperative task knows exactly where it can lose the CPU, so a read-modify-write on a shared variable is atomic as long as no yield sits in the middle. That is a real advantage on small parts, where every mutex costs RAM and every lock is a place for a deadlock.
A hybrid that works well
You do not have to pick one for the whole system. FreeRTOS preemption is configured globally, but you can reproduce cooperative behavior where you need it by giving cooperating tasks the same priority and turning time slicing off:
#define configUSE_PREEMPTION 1
#define configUSE_TIME_SLICING 0Now a higher-priority task still preempts a lower one, which keeps urgent work responsive. Tasks at the same priority never preempt each other, so they switch only when one blocks or yields. Groups of related tasks can share data without locks as long as they share a priority, which is a design technique worth using deliberately. It deserves a comment in the code, because the safety depends on a configuration constant three files away.
A concrete race under each mode
The difference is easiest to feel in a shared counter. Two tasks increment g_events, a 32-bit global, with a plain g_events++. On Cortex-M that compiles to a load, an add and a store: three instructions, with a window between the load and the store.
volatile uint32_t g_events;
static void vCounterTask(void *pvParameters)
{
(void) pvParameters;
for (;;)
{
g_events++;
vTaskDelay(1);
}
}Under cooperative scheduling this is safe, because the task cannot lose the CPU between the load and the store. Under preemption it is a race: if the tick interrupt lands between the load and the store and the other task runs, one increment overwrites the other. The failure is rare, which makes it worse. A test that runs for an hour might never show it, and a customer's device finds it in a week.
The fix under preemption is one of three tools. A critical section, taskENTER_CRITICAL() and taskEXIT_CRITICAL(), masks interrupts for the duration and suits a few instructions. A mutex suits longer sections that may block. A queue or a task notification avoids sharing altogether by sending events to a single owner task. We prefer the third wherever it fits, because it removes the question of who may touch the variable.
Reading a multi-word value is the cousin of this bug. A 64-bit timestamp updated by an interrupt and read by a task is two loads on a 32-bit core, and a preemption between them returns a torn value. The cooperative scheduler does not protect you here, because the interrupt still runs between the two loads. The only difference is whether a task could be the one that interferes.
Interrupts do not change the picture
In both modes, interrupts preempt tasks. What differs is what happens on return. In a preemptive kernel, an interrupt that unblocks a higher-priority task requests a switch, and the kernel honors it on exit. In cooperative mode, FreeRTOS marks the task ready but leaves the current task running until it yields. That makes cooperative systems simpler to reason about and much slower to react, because the interrupt's work waits for a yield you do not control.
Choosing
Prefer preemptive scheduling when any activity has a deadline shorter than the longest piece of code another task runs without blocking, which is true in almost every non-trivial design. It also protects you from a single unbounded loop, since higher-priority tasks keep running.
Consider cooperative scheduling when the system is tiny, the tasks are short and well known, RAM for locks is scarce, or you are porting a legacy superloop and want to move step by step. A cooperative kernel can be a half-way house on the way to preemption.
The honest limit of the hybrid: it protects only against tasks at equal priority. An interrupt handler can still modify the shared data at any moment, so ISR-to-task sharing needs a queue or a notification regardless of mode.


