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..8fd915007e70a0ace2accb0155e99ab545a43fa0 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`取消调度器的钩子函数。 + 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 318b4a03fdd929dd17629dea32b7731a6907b23c..d2d3622d665fbf135e670ac60dded4a16fccf10c 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, /* 超时时回调的处理函数 */