Notes on learning RT-Thread——线程应用示例

古城微笑少年丶 2022-02-15 02:30 458阅读 0赞

创建线程

  1. /*
  2. * 程序清单:创建/删除、初始化线程
  3. *
  4. * 这个例子会创建两个线程,一个动态线程,一个静态线程。
  5. * 一个线程在运行完毕后自动被系统删除,另一个线程一直打印计数。
  6. */
  7. #include <rtthread.h>
  8. #define THREAD_PRIORITY 25
  9. #define THREAD_STACK_SIZE 512
  10. #define THREAD_TIMESLICE 5
  11. static rt_thread_t tid1 = RT_NULL;
  12. /* 线程1的入口函数 */
  13. static void thread1_entry(void *parameter)
  14. {
  15. rt_uint32_t count = 0;
  16. while (1)
  17. {
  18. /* 线程1采用低优先级运行,一直打印计数值 */
  19. rt_kprintf("thread1 count: %d\n", count ++);
  20. rt_thread_mdelay(500);
  21. }
  22. }
  23. ALIGN(RT_ALIGN_SIZE)
  24. static char thread2_stack[1024];
  25. static struct rt_thread thread2;
  26. /* 线程2入口 */
  27. static void thread2_entry(void *param)
  28. {
  29. rt_uint32_t count = 0;
  30. /* 线程2拥有较高的优先级,以抢占线程1而获得执行 */
  31. for (count = 0; count < 10 ; count++)
  32. {
  33. /* 线程2打印计数值 */
  34. rt_kprintf("thread2 count: %d\n", count);
  35. }
  36. rt_kprintf("thread2 exit\n");
  37. /* 线程2运行结束后也将自动被系统删除
  38. (线程控制块和线程栈依然在idle线程中释放) */
  39. }
  40. /* 删除线程示例的初始化 */
  41. int thread_sample(void)
  42. {
  43. /* 创建线程1,名称是thread1,入口是thread1_entry*/
  44. tid1 = rt_thread_create("thread1",
  45. thread1_entry, RT_NULL,
  46. THREAD_STACK_SIZE,
  47. THREAD_PRIORITY, THREAD_TIMESLICE);
  48. /* 如果获得线程控制块,启动这个线程 */
  49. if (tid1 != RT_NULL)
  50. rt_thread_startup(tid1);
  51. /* 初始化线程2,名称是thread2,入口是thread2_entry */
  52. rt_thread_init(&thread2,
  53. "thread2",
  54. thread2_entry,
  55. RT_NULL,
  56. &thread2_stack[0],
  57. sizeof(thread2_stack),
  58. THREAD_PRIORITY - 1, THREAD_TIMESLICE);
  59. rt_thread_startup(&thread2);
  60. return 0;
  61. }
  62. /* 导出到 msh 命令列表中 */
  63. MSH_CMD_EXPORT(thread_sample, thread sample);

功能:创建一个动态线程初始化一个静态线程,一个线程在运行完毕后自动被系统删除,另一个线程一直打印计数
执行效果:
效果线程2在计10个数之后退出并被系统删除,而线程1一直在计数。

注意事项

关于删除线程:大多数线程是循环执行的,无需删除;而能运行完毕的线程,RT-Thread 在线程运行完毕后,自动删除线程,在 rt_thread_exit() 里完成删除动作。用户只需要了解该接口的作用,不推荐使用该接口(可以由其他线程调用此接口或在定时器超时函数中调用此接口删除一个线程,但是这种使用非常少)。

线程时间片轮转调度示例

  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-08-24 yangjie the first version
  9. */
  10. /*
  11. * 程序清单:相同优先级线程按照时间片轮番调度
  12. *
  13. * 这个例子中将创建两个线程,每一个线程都在打印信息
  14. *
  15. */
  16. #include <rtthread.h>
  17. #define THREAD_STACK_SIZE 1024
  18. #define THREAD_PRIORITY 20
  19. #define THREAD_TIMESLICE 10
  20. /* 线程入口 */
  21. static void thread_entry(void* parameter)
  22. {
  23. rt_uint32_t value;
  24. rt_uint32_t count = 0;
  25. value = (rt_uint32_t)parameter;
  26. while (1)
  27. {
  28. if(0 == (count % 5))
  29. {
  30. rt_kprintf("thread %d is running ,thread %d count = %d\n", value , value , count);
  31. if(count > 50)
  32. return;
  33. }
  34. count++;
  35. }
  36. }
  37. int timeslice_sample(void)
  38. {
  39. rt_thread_t tid;
  40. /* 创建线程1 */
  41. tid = rt_thread_create("thread1",
  42. thread_entry, (void*)1,
  43. THREAD_STACK_SIZE,
  44. THREAD_PRIORITY, THREAD_TIMESLICE);
  45. if (tid != RT_NULL)
  46. rt_thread_startup(tid);
  47. /* 创建线程2 */
  48. tid = rt_thread_create("thread2",
  49. thread_entry, (void*)2,
  50. THREAD_STACK_SIZE,
  51. THREAD_PRIORITY, THREAD_TIMESLICE-5);
  52. if (tid != RT_NULL)
  53. rt_thread_startup(tid);
  54. return 0;
  55. }
  56. /* 导出到 msh 命令列表中 */
  57. MSH_CMD_EXPORT(timeslice_sample, timeslice sample);

仿真效果:
效果线程2运行的时间是线程1的一半。

线程调度器钩子示例

在线程进行调度切换时,会执行调度,我们可以设置一个调度器钩子,这样可以在线程切换时,做一些额外的事情,这个例子是在调度器钩子函数中打印线程间的切换信息,如下代码:

  1. #include <rtthread.h>
  2. #define THREAD_STACK_SIZE 1024
  3. #define THREAD_PRIORITY 20
  4. #define THREAD_TIMESLICE 10
  5. /* 针对每个线程的计数器 */
  6. volatile rt_uint32_t count[2];
  7. /* 线程 1、2 共用一个入口,但入口参数不同 */
  8. static void thread_entry(void* parameter)
  9. {
  10. rt_uint32_t value;
  11. value = (rt_uint32_t)parameter;
  12. while (1)
  13. {
  14. rt_kprintf("thread %d is running\n", value);
  15. rt_thread_mdelay(1000); // 延时一段时间
  16. }
  17. }
  18. static rt_thread_t tid1 = RT_NULL;
  19. static rt_thread_t tid2 = RT_NULL;
  20. static void hook_of_scheduler(struct rt_thread* from, struct rt_thread* to)
  21. {
  22. rt_kprintf("from: %s --> to: %s \n", from->name , to->name);
  23. }
  24. int scheduler_hook(void)
  25. {
  26. /* 设置调度器钩子 */
  27. rt_scheduler_sethook(hook_of_scheduler);
  28. /* 创建线程 1 */
  29. tid1 = rt_thread_create("thread1",
  30. thread_entry, (void*)1,
  31. THREAD_STACK_SIZE,
  32. THREAD_PRIORITY, THREAD_TIMESLICE);
  33. if (tid1 != RT_NULL)
  34. rt_thread_startup(tid1);
  35. /* 创建线程 2 */
  36. tid2 = rt_thread_create("thread2",
  37. thread_entry, (void*)2,
  38. THREAD_STACK_SIZE,
  39. THREAD_PRIORITY,THREAD_TIMESLICE - 5);
  40. if (tid2 != RT_NULL)
  41. rt_thread_startup(tid2);
  42. return 0;
  43. }
  44. /* 导出到 msh 命令列表中 */
  45. MSH_CMD_EXPORT(scheduler_hook, scheduler_hook sample);

仿真效果:
在这里插入图片描述在这里插入图片描述由仿真的结果可以看出,对线程进行切换时,设置的调度器钩子函数是在正常工作的,一直在打印线程切换的信息,包含切换到空闲线程。

发表评论

表情:
评论列表 (有 0 条评论,458人围观)

还没有评论,来说两句吧...

相关阅读