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/3] =?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 cd6fe8eb9a58bcb2a1a06202dbd9d74a60b74bee Mon Sep 17 00:00:00 2001 From: CYFS <2805686936@qq.com> Date: Sun, 8 Sep 2024 23:32:22 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=BA=BF=E7=A8=8B?= =?UTF-8?q?=E7=A4=BA=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../programming-manual/thread/thread.md | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 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 8fd9150..acc21ce 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,7 +482,7 @@ void hook(struct rt_thread* from, struct rt_thread* to); ### 创建线程示例 -这个例子创建一个动态线程初始化一个静态线程,一个线程在运行完毕后自动被系统删除,另一个线程才能运行打印(优先级比较低),如下代码: +这个例子会创建一个动态线程和一个静态线程,当这个静态线程完成其任务并被系统自动回收后,具有较低优先级的动态线程才能开始运行并打印信息。 > 注意:RT-Thread 5.0 及更高的版本将 `ALIGN` 关键字改成了 `rt_align`,使用时注意。 @@ -501,15 +501,16 @@ static void thread1_entry(void *parameter) rt_uint32_t count = 0; for (count = 0; count < 10 ; count++) - { - /* 线程 1 采用低优先级运行 */ - rt_kprintf("thread1 count: %d\n", count); - rt_thread_mdelay(500); - } + { + /* 线程 1 采用低优先级运行 */ + rt_kprintf("thread1 count: %d\n", count); + rt_thread_mdelay(500); + } rt_kprintf("thread1 exit\n"); /* 线程 1 运行结束后也将自动被系统脱离 */ } -#if RT_VERSION_MAJOR >= 5 + +#if defined(RT_VERSION_CHECK) && (RTTHREAD_VERSION >= RT_VERSION_CHECK(5, 0, 1)) rt_align(RT_ALIGN_SIZE) #else ALIGN(RT_ALIGN_SIZE) @@ -615,7 +616,6 @@ static void thread_entry(void* parameter) if(0 == (count % 5)) { rt_kprintf("thread %d is running ,thread %d count = %d\n", value , value , count); - if(count> 200) return; } @@ -704,7 +704,7 @@ volatile rt_uint32_t count[2]; static void thread_entry(void* parameter) { rt_uint32_t value; - + value = (rt_uint32_t)parameter; for (int count = 0; count < 10 ; count++) { @@ -718,10 +718,10 @@ 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); + #if defined(RT_VERSION_CHECK) && (RTTHREAD_VERSION >= RT_VERSION_CHECK(5, 0, 1)) + 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); + rt_kprintf("from: %s --> to: %s \n", from->name , to->name); #endif } @@ -751,14 +751,14 @@ int scheduler_hook(void) /* 导出到 msh 命令列表中 */ MSH_CMD_EXPORT(scheduler_hook, scheduler_hook sample); -int s_del(void) +int scheduler_del(void) { rt_scheduler_sethook(RT_NULL); return 0; } /* 导出到 msh 命令列表中 */ -MSH_CMD_EXPORT(s_del, scheduler_del sample); +MSH_CMD_EXPORT(scheduler_del, scheduler_del sample); ``` 仿真运行结果如下: @@ -783,5 +783,5 @@ from: thread2 --> to: tidle … ``` -由仿真的结果可以看出,对线程进行切换时,设置的调度器钩子函数是在正常工作的,一直在打印线程切换的信息,包含切换到空闲线程。可以使用`s_del`取消调度器的钩子函数。 +由仿真的结果可以看出,对线程进行切换时,设置的调度器钩子函数是在正常工作的,一直在打印线程切换的信息,包含切换到空闲线程。可以使用`scheduler_del`取消调度器的钩子函数。 -- Gitee From 448eb7b13bed36b8f87be998507745755dc33731 Mon Sep 17 00:00:00 2001 From: CYFS <2805686936@qq.com> Date: Sun, 8 Sep 2024 23:36:12 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E7=A4=BA=E4=BE=8B=E7=BC=A9=E8=BF=9B=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../programming-manual/thread/thread.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 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 acc21ce..032ec76 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 @@ -500,13 +500,13 @@ static void thread1_entry(void *parameter) { rt_uint32_t count = 0; - for (count = 0; count < 10 ; count++) - { - /* 线程 1 采用低优先级运行 */ - rt_kprintf("thread1 count: %d\n", count); - rt_thread_mdelay(500); + for (count = 0; count < 10 ; count++) + { + /* 线程 1 采用低优先级运行 */ + rt_kprintf("thread1 count: %d\n", count); + rt_thread_mdelay(500); } - rt_kprintf("thread1 exit\n"); + rt_kprintf("thread1 exit\n"); /* 线程 1 运行结束后也将自动被系统脱离 */ } -- Gitee