thread_detach.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. *
  8. */
  9. /*
  10. * 程序清单:线程脱离
  11. *
  12. * 这个例子会创建两个线程,在其中一个线程中执行对另一个线程的脱离。
  13. */
  14. #include <rtthread.h>
  15. #include "tc_comm.h"
  16. /* 线程1控制块 */
  17. static struct rt_thread thread1;
  18. /* 线程1栈 */
  19. static rt_uint8_t thread1_stack[THREAD_STACK_SIZE];
  20. /* 线程2控制块 */
  21. static struct rt_thread thread2;
  22. /* 线程2栈 */
  23. static rt_uint8_t thread2_stack[THREAD_STACK_SIZE];
  24. /* 线程1入口 */
  25. static void thread1_entry(void* parameter)
  26. {
  27. rt_uint32_t count = 0;
  28. while (1)
  29. {
  30. /* 线程1采用低优先级运行,一直打印计数值 */
  31. rt_kprintf("thread count: %d\n", count ++);
  32. }
  33. }
  34. /* 线程2入口 */
  35. static void thread2_entry(void* parameter)
  36. {
  37. /* 线程2拥有较高的优先级,以抢占线程1而获得执行 */
  38. /* 线程2启动后先睡眠10个OS Tick */
  39. rt_thread_delay(10);
  40. /*
  41. * 线程2唤醒后直接执行线程1脱离,线程1将从就绪线程队列中删除
  42. */
  43. rt_thread_detach(&thread1);
  44. /*
  45. * 线程2继续休眠10个OS Tick然后退出
  46. */
  47. rt_thread_delay(10);
  48. /*
  49. * 线程2运行结束后也将自动被从就绪队列中删除,并脱离线程队列
  50. */
  51. }
  52. int thread_detach_init()
  53. {
  54. rt_err_t result;
  55. /* 初始化线程1 */
  56. result = rt_thread_init(&thread1, "t1", /* 线程名:t1 */
  57. thread1_entry, RT_NULL, /* 线程的入口是thread1_entry,入口参数是RT_NULL*/
  58. &thread1_stack[0], sizeof(thread1_stack), /* 线程栈是thread1_stack */
  59. THREAD_PRIORITY, 10);
  60. if (result == RT_EOK) /* 如果返回正确,启动线程1 */
  61. rt_thread_startup(&thread1);
  62. else
  63. tc_stat(TC_STAT_END | TC_STAT_FAILED);
  64. /* 初始化线程2 */
  65. result = rt_thread_init(&thread2, "t2", /* 线程名:t2 */
  66. thread2_entry, RT_NULL, /* 线程的入口是thread2_entry,入口参数是RT_NULL*/
  67. &thread2_stack[0], sizeof(thread2_stack), /* 线程栈是thread2_stack */
  68. THREAD_PRIORITY - 1, 10);
  69. if (result == RT_EOK) /* 如果返回正确,启动线程2 */
  70. rt_thread_startup(&thread2);
  71. else
  72. tc_stat(TC_STAT_END | TC_STAT_FAILED);
  73. return 0;
  74. }
  75. #ifdef RT_USING_TC
  76. static void _tc_cleanup()
  77. {
  78. /* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */
  79. rt_enter_critical();
  80. /* 执行线程脱离 */
  81. if (thread1.stat != RT_THREAD_CLOSE)
  82. rt_thread_detach(&thread1);
  83. if (thread2.stat != RT_THREAD_CLOSE)
  84. rt_thread_detach(&thread2);
  85. /* 调度器解锁 */
  86. rt_exit_critical();
  87. /* 设置TestCase状态 */
  88. tc_done(TC_STAT_PASSED);
  89. }
  90. int _tc_thread_detach()
  91. {
  92. /* 设置TestCase清理回调函数 */
  93. tc_cleanup(_tc_cleanup);
  94. thread_detach_init();
  95. /* 返回TestCase运行的最长时间 */
  96. return 25;
  97. }
  98. /* 输出函数命令到finsh shell中 */
  99. FINSH_FUNCTION_EXPORT(_tc_thread_detach, a static thread example);
  100. #else
  101. /* 用户应用入口 */
  102. int rt_application_init()
  103. {
  104. thread_detach_init();
  105. return 0;
  106. }
  107. #endif