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 a45b13c559dc00797c51e1bf95deca7f76275163..91caefd79929d69869899fc82b83f3c7fb930950 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,21 @@ 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 运行结束后也将自动被系统脱离 */ } -ALIGN(RT_ALIGN_SIZE) +#if defined(RT_VERSION_CHECK) && (RTTHREAD_VERSION >= RT_VERSION_CHECK(5, 0, 1)) + rt_align(RT_ALIGN_SIZE) +#else + ALIGN(RT_ALIGN_SIZE) +#endif static char thread2_stack[1024]; static struct rt_thread thread2; /* 线程 2 入口 */ @@ -582,7 +588,7 @@ thread1 count: 3 … ``` -线程 2 计数到一定值会执行完毕,线程 2 被系统自动删除,计数停止。线程 1 一直打印计数。 +线程 2 计数到一定值会执行完毕,线程 2 被系统自动删除,计数停止。线程 1 才会打印计数。 > [!NOTE] > 注:关于删除线程:大多数线程是循环执行的,无需删除;而能运行完毕的线程,RT-Thread 在线程运行完毕后,自动删除线程,在 rt_thread_exit() 里完成删除动作。用户只需要了解该接口的作用,不推荐使用该接口(可以由其他线程调用此接口或在定时器超时函数中调用此接口删除一个线程,但是这种使用非常少)。 @@ -610,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; } @@ -699,9 +704,9 @@ volatile rt_uint32_t count[2]; 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); // 延时一段时间 @@ -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 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); +#endif } int scheduler_hook(void) @@ -741,6 +750,15 @@ int scheduler_hook(void) /* 导出到 msh 命令列表中 */ MSH_CMD_EXPORT(scheduler_hook, scheduler_hook sample); + +int scheduler_del(void) +{ + rt_scheduler_sethook(RT_NULL); + return 0; +} + +/* 导出到 msh 命令列表中 */ +MSH_CMD_EXPORT(scheduler_del, scheduler_del sample); ``` 仿真运行结果如下: @@ -765,4 +783,5 @@ from: thread2 --> to: tidle … ``` -由仿真的结果可以看出,对线程进行切换时,设置的调度器钩子函数是在正常工作的,一直在打印线程切换的信息,包含切换到空闲线程。 +由仿真的结果可以看出,对线程进行切换时,设置的调度器钩子函数是在正常工作的,一直在打印线程切换的信息,包含切换到空闲线程。可以使用`scheduler_del`取消调度器的钩子函数。 +