xTaskNotify() with value in FreeRTOS - Time & Space Complexity
We want to understand how the time cost changes when using xTaskNotify() with a value in FreeRTOS.
Specifically, how does the execution time grow as the number of notifications or tasks changes?
Analyze the time complexity of the following code snippet.
BaseType_t xTaskNotify(
TaskHandle_t xTaskToNotify,
uint32_t ulValue,
eNotifyAction eAction
);
This function sends a notification with a value to a specific task, updating its notification state.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Updating the notification value and checking the task state.
- How many times: This happens once per call; no loops or recursion inside
xTaskNotify().
The execution time stays about the same no matter how many notifications are sent or how many tasks exist.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Constant steps to update one task |
| 100 | Same constant steps for one task |
| 1000 | Still constant steps for one task |
Pattern observation: The time does not grow with input size; it stays constant.
Time Complexity: O(1)
This means the time to notify a task with a value stays the same no matter how many tasks or notifications exist.
[X] Wrong: "Notifying a task takes longer if many tasks exist or many notifications are sent."
[OK] Correct: The function targets one task directly and updates its notification value in constant time, so other tasks or notifications do not affect this time.
Understanding that task notification functions run in constant time helps you reason about real-time system responsiveness and efficient task communication.
"What if xTaskNotify() was changed to notify multiple tasks at once? How would the time complexity change?"