0
0
FreeRTOSprogramming~10 mins

vTaskDelete() for task removal in FreeRTOS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - vTaskDelete() for task removal
Create Task
Task Running
Call vTaskDelete(task_handle)
Task Removed from Scheduler
Task Resources Freed
Task No Longer Runs
This flow shows how a task is created, runs, then is deleted using vTaskDelete(), which removes it from the scheduler and frees its resources.
Execution Sample
FreeRTOS
void Task1(void *pvParameters) {
  while(1) {
    // do work
    vTaskDelete(NULL); // delete self
  }
}
This code shows a task that deletes itself by calling vTaskDelete(NULL) inside its loop.
Execution Table
StepActionTask StateScheduler StatusOutput/Effect
1Task1 created and startedReadyTask1 scheduled to runTask1 will run when scheduled
2Task1 starts runningRunningTask1 runningTask1 executes code inside loop
3vTaskDelete(NULL) called inside Task1DeletingTask1 removed from schedulerTask1 will stop running
4Task1 resources freedDeletedNo longer scheduledTask1 no longer exists
5Scheduler runs other tasksOther tasks runningTask1 goneSystem continues without Task1
💡 Task1 calls vTaskDelete(NULL), so it is removed and deleted, stopping its execution.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
Task1 StateNot createdRunningDeletingDeleted
SchedulerNo tasksTask1 scheduledTask1 removedTask1 gone
Key Moments - 3 Insights
Why do we pass NULL to vTaskDelete() to delete the current task?
Passing NULL tells vTaskDelete() to delete the task that calls it, as shown in execution_table step 3 where Task1 deletes itself.
What happens if we delete a task that is currently running?
The task is marked for deletion and removed from the scheduler immediately, stopping its execution as shown in step 3 and 4.
Does vTaskDelete() free the task's memory automatically?
Yes, after removal from the scheduler, the task's resources are freed as shown in step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the task state immediately after calling vTaskDelete(NULL)?
ARunning
BReady
CDeleting
DDeleted
💡 Hint
Check execution_table row 3 under 'Task State' column.
At which step does the task stop being scheduled by the FreeRTOS scheduler?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at 'Scheduler Status' column in execution_table rows 2 and 3.
If we replace vTaskDelete(NULL) with vTaskDelete(task_handle) for another task, what changes in the execution flow?
AThe specified task is deleted instead of the current task
BThe current task deletes itself as before
CNo task is deleted
DThe scheduler stops all tasks
💡 Hint
vTaskDelete deletes the task given by the handle, not necessarily the caller; see concept_flow.
Concept Snapshot
vTaskDelete(task_handle) removes a task from FreeRTOS scheduler.
Pass NULL to delete the current task.
Task is removed immediately and resources freed.
Deleted tasks stop running and cannot be resumed.
Use carefully to avoid deleting wrong tasks.
Full Transcript
This visual execution shows how vTaskDelete() removes a task in FreeRTOS. First, a task is created and scheduled. When the task calls vTaskDelete(NULL), it deletes itself by removing from the scheduler and freeing resources. The task state changes from running to deleting, then deleted. The scheduler no longer runs the deleted task. Passing NULL deletes the current task; passing a task handle deletes that specific task. This stops the task from running and frees memory. The execution table and variable tracker show these state changes step-by-step.