From 57fa5ec46a23fb9aa8bbf7c6e2726f33e3ee19fd Mon Sep 17 00:00:00 2001 From: CYFS <2805686936@qq.com> Date: Tue, 3 Sep 2024 11:24:11 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=BA=BF=E7=A8=8B?= =?UTF-8?q?=E7=A4=BA=E4=BE=8B=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../programming-manual/thread/thread.md | 41 ++++++++++++++----- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/rt-thread-version/rt-thread-standard/programming-manual/thread/thread.md b/rt-thread-version/rt-thread-standard/programming-manual/thread/thread.md index a45b13c..8fd9150 100644 --- a/rt-thread-version/rt-thread-standard/programming-manual/thread/thread.md +++ b/rt-thread-version/rt-thread-standard/programming-manual/thread/thread.md @@ -482,9 +482,9 @@ void hook(struct rt_thread* from, struct rt_thread* to); ### 创建线程示例 -这个例子创建一个动态线程初始化一个静态线程,一个线程在运行完毕后自动被系统删除,另一个线程一直打印计数,如下代码: +这个例子创建一个动态线程初始化一个静态线程,一个线程在运行完毕后自动被系统删除,另一个线程才能运行打印(优先级比较低),如下代码: -> 注意:RT-Thread 5.0 及更高的版本将 `ALIGN` 关键字改成了 `rt_align`,使用时注意修改。 +> 注意:RT-Thread 5.0 及更高的版本将 `ALIGN` 关键字改成了 `rt_align`,使用时注意。 ```c #include @@ -500,15 +500,20 @@ static void thread1_entry(void *parameter) { rt_uint32_t count = 0; - while (1) + for (count = 0; count < 10 ; count++) { - /* 线程 1 采用低优先级运行,一直打印计数值 */ - rt_kprintf("thread1 count: %d\n", count ++); + /* 线程 1 采用低优先级运行 */ + rt_kprintf("thread1 count: %d\n", count); rt_thread_mdelay(500); } + rt_kprintf("thread1 exit\n"); + /* 线程 1 运行结束后也将自动被系统脱离 */ } - +#if RT_VERSION_MAJOR >= 5 +rt_align(RT_ALIGN_SIZE) +#else ALIGN(RT_ALIGN_SIZE) +#endif static char thread2_stack[1024]; static struct rt_thread thread2; /* 线程 2 入口 */ @@ -582,7 +587,7 @@ thread1 count: 3 … ``` -线程 2 计数到一定值会执行完毕,线程 2 被系统自动删除,计数停止。线程 1 一直打印计数。 +线程 2 计数到一定值会执行完毕,线程 2 被系统自动删除,计数停止。线程 1 才会打印计数。 > [!NOTE] > 注:关于删除线程:大多数线程是循环执行的,无需删除;而能运行完毕的线程,RT-Thread 在线程运行完毕后,自动删除线程,在 rt_thread_exit() 里完成删除动作。用户只需要了解该接口的作用,不推荐使用该接口(可以由其他线程调用此接口或在定时器超时函数中调用此接口删除一个线程,但是这种使用非常少)。 @@ -701,10 +706,10 @@ static void thread_entry(void* parameter) rt_uint32_t value; value = (rt_uint32_t)parameter; - while (1) + for (int count = 0; count < 10 ; count++) { - rt_kprintf("thread %d is running\n", value); - rt_thread_mdelay(1000); // 延时一段时间 + rt_kprintf("thread %d is running\n", value); + rt_thread_mdelay(1000); // 延时一段时间 } } @@ -713,7 +718,11 @@ static rt_thread_t tid2 = RT_NULL; static void hook_of_scheduler(struct rt_thread* from, struct rt_thread* to) { + #if RT_ALIGN_SIZE >= 5 + rt_kprintf("from: %s --> to: %s \n", from->parent.name ,to->parent.name); + #else rt_kprintf("from: %s --> to: %s \n", from->name , to->name); + #endif } int scheduler_hook(void) @@ -741,6 +750,15 @@ int scheduler_hook(void) /* 导出到 msh 命令列表中 */ MSH_CMD_EXPORT(scheduler_hook, scheduler_hook sample); + +int s_del(void) +{ + rt_scheduler_sethook(RT_NULL); + return 0; +} + +/* 导出到 msh 命令列表中 */ +MSH_CMD_EXPORT(s_del, scheduler_del sample); ``` 仿真运行结果如下: @@ -765,4 +783,5 @@ from: thread2 --> to: tidle … ``` -由仿真的结果可以看出,对线程进行切换时,设置的调度器钩子函数是在正常工作的,一直在打印线程切换的信息,包含切换到空闲线程。 +由仿真的结果可以看出,对线程进行切换时,设置的调度器钩子函数是在正常工作的,一直在打印线程切换的信息,包含切换到空闲线程。可以使用`s_del`取消调度器的钩子函数。 + -- Gitee From 5b88073feaf5ba0318c83e1c7a39e61f85dc1941 Mon Sep 17 00:00:00 2001 From: CYFS <2805686936@qq.com> Date: Tue, 3 Sep 2024 22:01:35 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=AE=9A=E6=97=B6?= =?UTF-8?q?=E5=99=A8=E7=A4=BA=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../rt-thread-standard/programming-manual/timer/timer.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/rt-thread-version/rt-thread-standard/programming-manual/timer/timer.md b/rt-thread-version/rt-thread-standard/programming-manual/timer/timer.md index 318b4a0..d2d3622 100644 --- a/rt-thread-version/rt-thread-standard/programming-manual/timer/timer.md +++ b/rt-thread-version/rt-thread-standard/programming-manual/timer/timer.md @@ -378,6 +378,7 @@ static void timeout2(void *parameter) int timer_sample(void) { + cnt = 0; /* 创建定时器 1 周期定时器 */ timer1 = rt_timer_create("timer1", timeout1, RT_NULL, 10, @@ -455,6 +456,7 @@ static void timeout2(void* parameter) int timer_static_sample(void) { + cnt = 0; /* 初始化定时器 */ rt_timer_init(&timer1, "timer1", /* 定时器名字是 timer1 */ timeout1, /* 超时时回调的处理函数 */ -- Gitee