freeRTOS Deleting a Task

vTaskDelete()

A task can use the vTaskDelete() API function to delete itself, or any other task. Note that the vTaskDelete() API function is available only when INCLUDE_vTaskDelete is set to 1 in FreeRTOSConfig.h.

Deleted tasks no longer exist and cannot enter the Running state again.

It is the responsibility of the idle task to free memory allocated to tasks that have since been deleted. Therefore, it is important that applications using the vTaskDelete() API function do not completely starve the idle task of all processing time.

Note: Only memory allocated to a task by the kernel itself will be freed automatically when the task is deleted. Any memory or other resource that the implementation of the task allocated must be freed explicitly.

void vTaskDelete( TaskHandle_t pxTaskToDelete );

pxTaskToDelete
The handle of the task that is to be deleted (the subject task)—see the pxCreatedTask parameter of the xTaskCreate() API function for information on obtaining handles to tasks.
A task can delete itself by passing NULL in place of a valid task handle.


Example

#include "FreeRTOS.h"
#include "task.h"
#include "stm32f4xx.h"
//Task1
// ----------------------------------------------------------------------------
TaskHandle_t xTask2Handle = NULL;
void vTask2( void *pvParameters )
{
uint16_t count2;
/* Task 2 does nothing but delete itself. To do this it could call vTaskDelete()
using NULL as the parameter, but instead, and purely for demonstration purposes,
it calls vTaskDelete() passing its own task handle. */
count2++; vTaskDelete( xTask2Handle );
}
void vTask1( void *pvParameters )
{
uint16_t count1;
const TickType_t xDelay100ms = pdMS_TO_TICKS( 100UL );
for( ;; )
{
/* Create task 2 at a higher priority. Again the task parameter is not
used so is set to NULL - BUT this time the task handle is required so
the address of xTask2Handle is passed as the last parameter. */
xTaskCreate( vTask2, "Task 2", 1000, NULL, 2, &xTask2Handle );
/* The task handle is the last parameter _____^^^^^^^^^^^^^ */
/* Task 2 has/had the higher priority, so for Task 1 to reach here Task 2
must have already executed and deleted itself. Delay for 100
milliseconds. */
count1++; vTaskDelay( xDelay100ms );
}
}
////Task2
//// ----------------------------------------------------------------------------
void GPIOInit(){
    GPIO_InitTypeDef g;
    g.GPIO_Pin = GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15; // ?? 12,13,14,15 ?
    g.GPIO_Mode = GPIO_Mode_OUT; // ???????
    g.GPIO_Speed = GPIO_Speed_100MHz; // ?? GPIO ???100 MHz
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); // ?? GPIOD
    GPIO_Init(GPIOD, &g); // ??? GPIO D
}

int main( void )
{
/* Create the first task at priority 1. The task parameter is not used
so is set to NULL. The task handle is also not used so likewise is set
to NULL. */
xTaskCreate( vTask1, "Task 1", 1000, NULL, 1, NULL );
/* The task is created at priority 1 ______^. */
/* Start the scheduler so the task starts executing. */
vTaskStartScheduler();
/* main() should never reach here as the scheduler has been started. */
for( ;; );
}
注意:
執行此程式需使用heap_2,heap_3或heap_4,heap_1不能釋放記憶體

留言

這個網誌中的熱門文章

05 Software Timer Management

Interrupt Management