Task pooling for dynamic workloads in FreeRTOS - Time & Space Complexity
When using task pooling in FreeRTOS, we want to know how the time to handle tasks changes as more tasks come in.
We ask: How does the system's work grow when the number of tasks increases?
Analyze the time complexity of the following code snippet.
// Simple task pool example
void TaskPoolHandler(void *params) {
while(1) {
TaskHandle_t task = GetNextTaskFromQueue();
if(task != NULL) {
ProcessTask(task);
}
vTaskDelay(10 / portTICK_PERIOD_MS);
}
}
This code runs a task pool handler that repeatedly checks for new tasks and processes them one by one.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The infinite loop that repeatedly fetches and processes tasks.
- How many times: It runs continuously, processing each task as it arrives.
As the number of tasks increases, the handler processes each task one after another.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 task process calls |
| 100 | About 100 task process calls |
| 1000 | About 1000 task process calls |
Pattern observation: The work grows directly with the number of tasks; more tasks mean more processing time.
Time Complexity: O(n)
This means the time to process tasks grows in a straight line with the number of tasks.
[X] Wrong: "The task pool processes all tasks instantly regardless of how many there are."
[OK] Correct: Each task still needs time to be processed one by one, so more tasks take more time overall.
Understanding how task pools handle growing workloads helps you explain system responsiveness and efficiency in real embedded projects.
"What if we added multiple worker tasks to process tasks in parallel? How would the time complexity change?"