Understanding RTOS Coroutines vs. Tasks: Concepts, Code Samples, and Key Differences
This article explains what coroutines are in an RTOS, describes their three states, provides step‑by‑step C code examples for creating and scheduling coroutines, and compares their resource usage, scheduling, and performance with traditional RTOS tasks, highlighting advantages and limitations.
In a typical Windows environment you can see many processes and threads, but an RTOS (Real‑Time Operating System) does not have processes; instead it uses tasks (threads) and a less‑known construct called coroutines .
What is a coroutine?
A coroutine, short for “co‑operative program”, is a lightweight concurrent programming model that runs multiple routines within a single thread. Each coroutine can be paused and later resumed , allowing many logical flows to share one CPU core.
Coroutine states
Running : the coroutine is currently executing on the processor.
Ready : the coroutine can run (not blocked) but is not currently executing.
Blocked : the coroutine is waiting for a time event or an external event.
Basic coroutine function structure
void vCoRoutineFunction(CoRoutineHandle_t xHandle, UBaseType_t uxIndex)
{
crSTART(xHandle);
for(;;)
{
// -- Co‑routine application code here. --
}
crEND();
}The function must start with crSTART() and end with crEND(); it never returns a value.
Example: LED‑blinking coroutine
1️⃣ Create a simple coroutine that toggles an LED after a delay:
void vFlashCoRoutine(CoRoutineHandle_t xHandle, UBaseType_t uxIndex)
{
crSTART(xHandle);
for(;;)
{
crDELAY(xHandle, 10); // delay
vParTestToggleLED(0); // toggle LED 0
}
crEND();
}2️⃣ Schedule the coroutine from the idle hook:
void vApplicationIdleHook(void)
{
vCoRoutineSchedule();
}3️⃣ Create the coroutine and start the scheduler in main():
#include "task.h"
#include "croutine.h"
#define PRIORITY_0 0
void main(void)
{
xCoRoutineCreate(vFlashCoRoutine, PRIORITY_0, 0);
vTaskStartScheduler();
}4️⃣ Extend to multiple coroutines using an index parameter. By creating eight coroutines, each can flash a different LED at a different rate:
#define PRIORITY_0 0
#define NUM_COROUTINES 8
void main(void)
{
for(int i = 0; i < NUM_COROUTINES; i++)
{
xCoRoutineCreate(vFlashCoRoutine, PRIORITY_0, i);
}
vTaskStartScheduler();
}
const int iFlashRate[NUM_COROUTINES] = {10,20,30,40,50,60,70,80};
const int iLEDToFlash[NUM_COROUTINES] = {0,1,2,3,4,5,6,7};
void vFlashCoRoutine(CoRoutineHandle_t xHandle, UBaseType_t uxIndex)
{
crSTART(xHandle);
for(;;)
{
crDELAY(xHandle, iFlashRate[uxIndex]);
vParTestToggleLED(iLEDToFlash[uxIndex]);
}
crEND();
}RTOS coroutine vs. task differences
Scheduling and management : Tasks are scheduled by the OS kernel; coroutines are managed manually by the application.
Resource consumption : Tasks require kernel resources and stack space; coroutines share a single thread’s stack, using far less RAM.
RAM usage : Because coroutines avoid per‑task stacks, they are suitable for memory‑constrained 8‑bit or 16‑bit MCUs.
Limitations : Coroutines have stricter constraints and more complex programming patterns compared with tasks.
Execution efficiency : Context switches are controlled by the program, eliminating the overhead of pre‑emptive thread switches, often resulting in higher throughput.
Early RTOSes such as FreeRTOS provided coroutine support, but as MCU resources grew, many developers switched to full‑featured tasks.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
