STM32之任务的创建与删除

目录

1. 什么是任务?

2. 任务创建与删除相关函数

任务动态创建与静态创建的区别:

 xTaskCreate 函数原型

官方案例: 

 vTaskDelete 函数原型

3. 实操


1. 什么是任务?

任务可以理解为进程 / 线程,创建一个任务,就会在内存开辟一个空间。
比如:
玩游戏、陪女朋友,都可以视为任务
Windows 系统中的 MarkText 、谷歌浏览器、记事本,都是任务。
任务通常都含有 while(1) 死循环。

2. 任务创建与删除相关函数

任务创建与删除相关函数有如下三个:

任务动态创建与静态创建的区别:

动态创建任务的堆栈由系统分配,而静态创建任务的堆栈由用户自己传递。
通常情况下使用动态方式创建任务。

 xTaskCreate 函数原型

1. pvTaskCode :指向任务函数的指针,任务必须实现为永不返回(即连续循环);
2. pcName :任务的名字,主要是用来调试,默认情况下最大长度是 16
3. pvParameters :指定的任务栈的大小;
4. uxPriority :任务优先级, 数值越大,优先级越大
5. pxCreatedTask :用于返回已创建任务的句柄可以被引用。

 

 

官方案例: 

/* Task to be created. */
void vTaskCode( void * pvParameters )
{
    /* The parameter value is expected to be 1 as 1 is passed in the
    pvParameters value in the call to xTaskCreate() below.
    configASSERT( ( ( uint32_t ) pvParameters ) == 1 );
    for( ;; )
    {
    /* Task code goes here. */
    }
}
/* Function that creates a task. */
void vOtherFunction( void )
{
    BaseType_t xReturned;
    TaskHandle_t xHandle = NULL;
    /* Create the task, storing the handle. */
    xReturned = xTaskCreate(
        vTaskCode, /* Function that implements the task. */
        "NAME", /* Text name for the task. */
        STACK_SIZE, /* Stack size in words, not bytes. */
        ( void * ) 1, /* Parameter passed into the task. */
        tskIDLE_PRIORITY,/* Priority at which the task is created. */
        &xHandle ); /* Used to pass out the created task's handle. */
    if( xReturned == pdPASS )
    {
        /* The task was created. Use the task's handle to delete the task. */
        vTaskDelete( xHandle );
    }    
}

 vTaskDelete 函数原型

void vTaskDelete(TaskHandle_t xTaskToDelete);

只需将待删除的任务句柄传入该函数,即可将该任务删除。
当传入的参数为 NULL ,则代表删除任务自身(当前正在运行的任务)。

3. 实操

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陈学弟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值