Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
夏令营
2024-RSOC
【2024-RSOC】RT-Thread Task的创建和删除 (Day 2 )
发布于 2024-07-24 03:50:17 浏览:165
订阅该版
[tocm] ## Task creation and using * In Day two, I will show how to use different way to create tasks and , how to use some of functions. --- ### 1- thread_creation.c **Short description** This task create two task by different way , static and dynamic. by using the functions of **rt_thread_init** and **rt_thread_create**
PS: Do not leave a while() to the end of main function scope. Otherwise, the task will never be scheduled
```c int main(void) { err = rt_thread_init(&thread1, "staticTask", staticTask, RT_NULL, static_stack, sizeof(static_stack), 25, 5); if (err == RT_EOK) { rt_thread_startup(&thread1); } thread2 = rt_thread_create("dynamicTask", dynamicTask, RT_NULL, 512, 30, 20); rt_thread_startup(thread2); while (1) { } } ``` The correct way is below: ```c int main(void) { err = rt_thread_init(&thread1, "staticTask", staticTask, RT_NULL, static_stack, sizeof(static_stack), 25, 5); if (err == RT_EOK) { rt_thread_startup(&thread1); } thread2 = rt_thread_create("dynamicTask", dynamicTask, RT_NULL, 512, 30, 20); rt_thread_startup(thread2); return 0; } ``` Effect like below (Correct Way ): ![screenshot_Effect.jpg](https://oss-club.rt-thread.org/uploads/20240724/258d94f2d28b1fba14b9c8d1de732914.jpg.webp) InCorrect Way: ![screenshot_incorrect.jpg](https://oss-club.rt-thread.org/uploads/20240724/a328600b2c2aaf21859b6fd7c22cc6d8.jpg.webp) ### 2- main.c at /application-day2 This C code demonstrate how to delete a task by using functions **rt_thread_detach** and **rt_thread_delete** **rt_thread_detach** is used to delete static task. **rt_thread_delete** can not delete static task **Delete dynamic task by using rt_thread_delete()** ```c /** * This task will be crated dynamically and deleted by rt_thread_delete once i == 10 * @param parameter */ void dynamicTask(void * parameter) { int i =0; while(1){ if(i==10){ rt_kprintf("Deleted dynamicTask Task\r\n"); rt_thread_t t = rt_thread_self(); rt_thread_delete(t); } rt_kprintf("This is a dynamicTask Task\r\n"); i++; rt_thread_delay(500); } } ``` **Delete dynamic task by using rt_thread_detach()** ```c /** * This task will deleted by rt_thread_detach once i == 10 * @param parameter */ static void staticTask(void * parameter) { int i =0; while(1){ if(i==10){ rt_kprintf("Deleted staticTask Task\r\n"); rt_thread_t t = rt_thread_self(); rt_thread_detach(t); } rt_kprintf("This is a staticTask Task\r\n"); i++; rt_thread_delay(500); } } ``` Effect: ![screenshot_thread.jpg](https://oss-club.rt-thread.org/uploads/20240724/7698116efa83808e74bdbf3861c375f0.jpg.webp)
0
条评论
默认排序
按发布时间排序
登录
注册新账号
关于作者
RT_Overflow
这家伙很懒,什么也没写!
文章
7
回答
16
被采纳
0
关注TA
发私信
相关文章
1
2024RT-Thread操作系统
推荐文章
1
RT-Thread应用项目汇总
2
玩转RT-Thread系列教程
3
国产MCU移植系列教程汇总,欢迎查看!
4
机器人操作系统 (ROS2) 和 RT-Thread 通信
5
五分钟玩转RT-Thread新社区
6
【技术三千问】之《玩转ART-Pi》,看这篇就够了!干货汇总
7
关于STM32H7开发板上使用SDIO接口驱动SD卡挂载文件系统的问题总结
8
STM32的“GPU”——DMA2D实例详解
9
RT-Thread隐藏的宝藏之completion
10
【ART-PI】RT-Thread 开启RTC 与 Alarm组件
热门标签
RT-Thread Studio
串口
Env
LWIP
SPI
AT
Bootloader
Hardfault
CAN总线
FinSH
ART-Pi
USB
DMA
文件系统
RT-Thread
SCons
RT-Thread Nano
线程
MQTT
STM32
RTC
FAL
rt-smart
I2C_IIC
ESP8266
UART
WIZnet_W5500
ota在线升级
cubemx
PWM
flash
freemodbus
BSP
packages_软件包
潘多拉开发板_Pandora
定时器
ADC
flashDB
GD32
socket
编译报错
中断
Debug
rt_mq_消息队列_msg_queue
SFUD
msh
keil_MDK
ulog
C++_cpp
MicroPython
本月问答贡献
xusiwei1236
8
个答案
2
次被采纳
踩姑娘的小蘑菇
1
个答案
2
次被采纳
用户名由3_15位
9
个答案
1
次被采纳
bernard
4
个答案
1
次被采纳
RTT_逍遥
3
个答案
1
次被采纳
本月文章贡献
聚散无由
2
篇文章
15
次点赞
catcatbing
2
篇文章
5
次点赞
Wade
2
篇文章
4
次点赞
Ghost_Girls
1
篇文章
7
次点赞
YZRD
1
篇文章
2
次点赞
回到
顶部
发布
问题
投诉
建议
回到
底部