In earlier tutorials, we saw how to create tasks and used.
In this tutorial, we will see how to delete a task once its job is done.


API Details

Here we will discuss some of the most frequently used APIs related to tasks.

1.xTaskCreate(): This interface is used to create a new Task, if the task is successfully created then it returns pdPass(1) or else errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY(-1). Check this link for more details.

2.vTaskDelay(): This function is used to delay/block the task for specified delay time(ticks). INCLUDE_vTaskDelay needs to be set to 1 in FreeRtosConfig.h file for using this function. Check this link for more details.

3.vTaskDelete():This function is used to delete as task. We need to pass the taskHandle of the task to be deleted.
To delete the own task we should pass NULL as parameter.
Please check this link for detials.

Example


Output

TaskDeleteUsage.jpg
0. Serial port is initialized and 3-Tasks are created with different priorities. Setup message is printed.

  1. Cpu chooses Task3 out of the 4-Tasks(1,2,3,and idle) as it has higher priority. Task3 will run for some time and goes to wait/blocked state for 150ms.
  2. At this 3-Tasks(1,2,idle) are available and scheduler chooses Task2. It runs for some time and goes to blocked state for 150ms.
  3. Now 2-Tasks(1,Idle) are available and Task1 runs because of its higher priority. It also goes to blocked state for 100ms
  4. Now scheduler is left out with Idle task and it keeps running till one of the tasks waiting time is elapsed and comes out of blocked state.
  5. Since Task1 was blocked for only 100ms, it will come out of the blocked state and starts running. And finally it deletes itself once it job is done.
  6. Now again Idle task will start running as there are no other tasks to run.
  7. At this point Task3 will come out of the waiting/blocked state and preempts the Idle task and starts running. It also deletes itself.
  8. By this time Task2 is also out of blocked state and runs for some time and deletes itself.
  9. Now there is only one task ie.The idle task as keeps running.