Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem TCP & FAT


Deleting a task and erasing it's handler

Posted by chaabanemalki on October 26, 2015


I'm trying to figure out a way on how to know if a task was created or not, and I did the following :

I have taskA and it's handler taskAHandler. I have 2 places to delete taskA : the first one inside the task itself, and the second place is in another task.

for both cases, I use this function :

TaskA_Delete() { if ( taskAHandler != NULL) vTaskDelete(taskAHandler); }

This works just fine inside the task itself or outside it. I want to chek if the task exists or not before deleting it, but it seems that the taskAHandler isn't set to NULL when its task is deleted !! Does the kernel erase the value on the task handler once the task is deleted ?

so I tried erasing the handler myself like this :

TaskA_Delete() { if ( taskAHandler != NULL) { vTaskDelete(taskAHandler); ** taskAHandler = NULL;** }

When I call the above fonction within taskA, the taskAHandler doesn't get erased. I assume that once the task is deleted, the execution of what follow is ignored ? am I right ?

Now, I can't really find a way to know if the task has been created or not ?

Any idea how to do that ?

Thank you.

Deleting a task and erasing it's handler

Posted by rtel on October 28, 2015

Sorry for the delay in replying, it appears several emails have got stuck in moderation over the last few days, even though the forum is set to automatically approve all posts from users who have logged in.

The task handles are variables like any other variable. They allow your application code to reference a task, but there is no link back the other way and FreeRTOS doesn't know anything about the variables, where the variable is allocated (stack, global, static, etc.) or how many copies of the variable you have made, or even if the variable still exists. So FreeRTOS cannot automatically set the variable back to NULL for you. If you need the variable to be set to NULL then it should be set manually after the task has been deleted - however that cannot be done if the task deletes itself because nothing after the call to vTaskDelete() will run (the task no longer exists so cannot be executing anything).

While a task cannot automatically clear a variable that points to it to NULL, it would be possible for the handle to be invalidated in some other way - for example for the data structures that describe the task to be updated to say the task is no longer valid. However that is not a practical option because every function that used a task handle would have to have code added to it to check the handle was valid or not, and once the task has been deleted the memory that was used by the task can be immediately re-used for any other purpose (including to hold data on a task that was subsequently created) so could hold any value anyway.

I would suggest your code is changed to something like the following - not the scheduler lock is used to ensure no other tasks try to use the task handle in the mean time:

~~~~ TaskADelete() { /* Remember the value of the variable as it will be set to NULL. */ TaskHandlet xTask = taskAHandler;


if( taskAHandler != NULL )
    /* The task is going to be deleted.
    Set the handle to NULL. */
    taskAHandler = NULL;

    /* Delete using the copy of the handle. */
    vTaskDelete( xTask );

} ~~~~

A task can delete itself by calling vTaskDelete( NULL );

[ Back to the top ]    [ About FreeRTOS ]    [ Sitemap ]    [ ]

Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.

Latest News

FreeRTOS kernel V10 is available for immediate download. Now MIT licensed.

FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

IAR Partner

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers

STMicro RTOS partner supporting ARM7, ARM Cortex-M3, ARM Cortex-M4 and ARM Cortex-M0

Texas Instruments MCU Developer Network RTOS partner for ARM and MSP430 microcontrollers

OpenRTOS and SafeRTOS