vTaskDelete() is used to stop and remove a task from running in FreeRTOS. It helps free up resources when a task is no longer needed.
vTaskDelete() for task removal in FreeRTOS
void vTaskDelete(TaskHandle_t xTaskToDelete);
If xTaskToDelete is NULL, the calling task deletes itself.
Deleted tasks will not run again unless recreated.
vTaskDelete(NULL);
vTaskDelete(xHandle);
This program creates two tasks. Task 1 starts and delays for 1000 ms but gets deleted by Task 2 before resuming and printing its deletion message. Task 2 delays for 500 ms, deletes Task 1, prints confirmation, then deletes itself. The print statements show the order of events.
#include "FreeRTOS.h" #include "task.h" #include <stdio.h> TaskHandle_t xTask1Handle = NULL; void vTask1(void *pvParameters) { printf("Task 1 started\n"); vTaskDelay(pdMS_TO_TICKS(1000)); printf("Task 1 deleting itself\n"); vTaskDelete(NULL); // Task deletes itself } void vTask2(void *pvParameters) { printf("Task 2 started\n"); vTaskDelay(pdMS_TO_TICKS(500)); printf("Task 2 deleting Task 1\n"); vTaskDelete(xTask1Handle); // Delete Task 1 printf("Task 2 running after deleting Task 1\n"); vTaskDelete(NULL); // Task 2 deletes itself } int main(void) { xTaskCreate(vTask1, "Task1", 1000, NULL, 1, &xTask1Handle); xTaskCreate(vTask2, "Task2", 1000, NULL, 1, NULL); vTaskStartScheduler(); // Should never reach here for(;;); return 0; }
Once a task is deleted, its stack and resources are freed by the kernel.
Do not use a deleted task handle after deletion; it becomes invalid.
Deleting the current task with vTaskDelete(NULL) stops it immediately.
vTaskDelete() removes a task from FreeRTOS to free resources.
Use NULL to delete the current running task.
Always ensure deleted tasks are not referenced again.
Practice
vTaskDelete() do?Solution
Step 1: Understand the purpose of
This function is designed to remove a task from FreeRTOS and free the memory and resources it used.vTaskDelete()Step 2: Compare with other task functions
Functions like suspending or pausing do not remove the task or free resources, onlyvTaskDelete()does that.Final Answer:
It removes a task from the scheduler and frees its resources. -> Option AQuick Check:
vTaskDelete() = Remove task and free resources [OK]
- Confusing deletion with suspension or pausing
- Thinking it creates or starts tasks
- Assuming it only stops task temporarily
Solution
Step 1: Recall how to delete the current task
PassingNULLtovTaskDelete()tells FreeRTOS to delete the task that is currently running.Step 2: Check other options
Options A and D use variables that may not be defined or correct. vTaskDelete(); misses the required parameter.Final Answer:
vTaskDelete(NULL); -> Option AQuick Check:
Delete current task = vTaskDelete(NULL) [OK]
- Omitting the parameter in vTaskDelete()
- Passing an undefined task handle
- Using variable names instead of NULL for current task
void Task1(void *pvParameters) {
// Task code
vTaskDelete(NULL);
}
void main() {
xTaskCreate(Task1, "Task1", 1000, NULL, 1, NULL);
vTaskStartScheduler();
}What happens when
Task1 runs?Solution
Step 1: Analyze
PassingvTaskDelete(NULL)inside Task1NULLdeletes the currently running task, which is Task1 itself.Step 2: Understand the effect on Task1
Task1 will remove itself from the scheduler and free its resources immediately after running.Final Answer:
Task1 deletes itself and frees its resources. -> Option CQuick Check:
vTaskDelete(NULL) inside task = self-delete [OK]
- Thinking NULL deletes a different task
- Assuming it causes an error with NULL
- Confusing deletion with suspension
TaskHandle_t xHandle = NULL; xTaskCreate(TaskFunction, "Task", 1000, NULL, 1, &xHandle); vTaskDelete(xHandle);
What is the problem with this code?
Solution
Step 1: Understand task creation and immediate deletion
The task is created and its handle stored inxHandle. Immediately deleting it may cause issues if the task has not started or scheduler not running.Step 2: Recognize timing and scheduler state
Deleting a task before the scheduler runs or before the task starts can cause undefined or unexpected behavior.Final Answer:
Deleting a task immediately after creation may cause undefined behavior. -> Option DQuick Check:
Delete right after create = possible undefined behavior [OK]
- Assuming NULL only deletes current task (it can delete any valid handle)
- Ignoring scheduler state before deleting tasks
- Thinking deletion does not remove task from scheduler
TaskToDelete from another task. Which approach is correct?Solution
Step 1: Identify how to delete a specific task from another task
To delete a specific task, you must pass its task handle tovTaskDelete(). The handle must be valid and known.Step 2: Check other options for correctness
PassingNULLdeletes the current running task, not another. Omitting parameters is invalid syntax.Final Answer:
Call vTaskDelete(TaskToDeleteHandle); directly from the other task after ensuring TaskToDeleteHandle is valid. -> Option BQuick Check:
Delete specific task = vTaskDelete(taskHandle) [OK]
- Using NULL to delete a different task
- Calling vTaskDelete() without parameters
- Not verifying task handle validity before deletion
Ready to prove it?
Pass the test and this pattern moves unknown
