Bird
Raised Fist0
FreeRTOSprogramming~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the FreeRTOS function vTaskDelete() do?
easy
A. It removes a task from the scheduler and frees its resources.
B. It pauses a task temporarily without deleting it.
C. It creates a new task and adds it to the scheduler.
D. It suspends all tasks except the idle task.

Solution

  1. Step 1: Understand the purpose of vTaskDelete()

    This function is designed to remove a task from FreeRTOS and free the memory and resources it used.
  2. Step 2: Compare with other task functions

    Functions like suspending or pausing do not remove the task or free resources, only vTaskDelete() does that.
  3. Final Answer:

    It removes a task from the scheduler and frees its resources. -> Option A
  4. Quick Check:

    vTaskDelete() = Remove task and free resources [OK]
Hint: vTaskDelete() means task removal and resource freeing [OK]
Common Mistakes:
  • Confusing deletion with suspension or pausing
  • Thinking it creates or starts tasks
  • Assuming it only stops task temporarily
2. Which of the following is the correct syntax to delete the currently running task in FreeRTOS?
easy
A. vTaskDelete(NULL);
B. vTaskDelete(xTaskHandle);
C. vTaskDelete();
D. vTaskDelete(currentTask);

Solution

  1. Step 1: Recall how to delete the current task

    Passing NULL to vTaskDelete() tells FreeRTOS to delete the task that is currently running.
  2. Step 2: Check other options

    Options A and D use variables that may not be defined or correct. vTaskDelete(); misses the required parameter.
  3. Final Answer:

    vTaskDelete(NULL); -> Option A
  4. Quick Check:

    Delete current task = vTaskDelete(NULL) [OK]
Hint: Use NULL to delete the running task in vTaskDelete() [OK]
Common Mistakes:
  • Omitting the parameter in vTaskDelete()
  • Passing an undefined task handle
  • Using variable names instead of NULL for current task
3. Consider the following FreeRTOS code snippet:
void Task1(void *pvParameters) {
    // Task code
    vTaskDelete(NULL);
}

void main() {
    xTaskCreate(Task1, "Task1", 1000, NULL, 1, NULL);
    vTaskStartScheduler();
}

What happens when Task1 runs?
medium
A. Task1 causes a runtime error due to NULL parameter.
B. Task1 deletes another task passed as NULL.
C. Task1 deletes itself and frees its resources.
D. Task1 suspends itself but remains in the scheduler.

Solution

  1. Step 1: Analyze vTaskDelete(NULL) inside Task1

    Passing NULL deletes the currently running task, which is Task1 itself.
  2. Step 2: Understand the effect on Task1

    Task1 will remove itself from the scheduler and free its resources immediately after running.
  3. Final Answer:

    Task1 deletes itself and frees its resources. -> Option C
  4. Quick Check:

    vTaskDelete(NULL) inside task = self-delete [OK]
Hint: vTaskDelete(NULL) inside task deletes that task itself [OK]
Common Mistakes:
  • Thinking NULL deletes a different task
  • Assuming it causes an error with NULL
  • Confusing deletion with suspension
4. Given this code snippet:
TaskHandle_t xHandle = NULL;
xTaskCreate(TaskFunction, "Task", 1000, NULL, 1, &xHandle);
vTaskDelete(xHandle);

What is the problem with this code?
medium
A. The task handle is NULL, so deletion fails.
B. The task is deleted but not removed from the scheduler.
C. vTaskDelete() requires NULL to delete the current task only.
D. Deleting a task immediately after creation may cause undefined behavior.

Solution

  1. Step 1: Understand task creation and immediate deletion

    The task is created and its handle stored in xHandle. Immediately deleting it may cause issues if the task has not started or scheduler not running.
  2. 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.
  3. Final Answer:

    Deleting a task immediately after creation may cause undefined behavior. -> Option D
  4. Quick Check:

    Delete right after create = possible undefined behavior [OK]
Hint: Avoid deleting tasks immediately after creation before scheduler runs [OK]
Common Mistakes:
  • 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
5. You have multiple tasks running and want to safely delete a task named TaskToDelete from another task. Which approach is correct?
hard
A. Call vTaskDelete(NULL); from the other task to delete TaskToDelete.
B. Call vTaskDelete(TaskToDeleteHandle); directly from the other task after ensuring TaskToDeleteHandle is valid.
C. Suspend TaskToDelete and then call vTaskDelete(NULL); from the other task.
D. Use vTaskDelete() without parameters to delete TaskToDelete.

Solution

  1. Step 1: Identify how to delete a specific task from another task

    To delete a specific task, you must pass its task handle to vTaskDelete(). The handle must be valid and known.
  2. Step 2: Check other options for correctness

    Passing NULL deletes the current running task, not another. Omitting parameters is invalid syntax.
  3. Final Answer:

    Call vTaskDelete(TaskToDeleteHandle); directly from the other task after ensuring TaskToDeleteHandle is valid. -> Option B
  4. Quick Check:

    Delete specific task = vTaskDelete(taskHandle) [OK]
Hint: Pass the task's handle to vTaskDelete() to remove it safely [OK]
Common Mistakes:
  • Using NULL to delete a different task
  • Calling vTaskDelete() without parameters
  • Not verifying task handle validity before deletion
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard