msgq_sample.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright (c) 2006-2022, 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. * 这个程序会创建2个动态线程,一个线程会从消息队列中收取消息;一个线程会定时给消
  14. * 息队列发送 普通消息和紧急消息。
  15. */
  16. #include <rtthread.h>
  17. #define THREAD_PRIORITY 25
  18. #define THREAD_TIMESLICE 5
  19. /* 消息队列控制块 */
  20. static struct rt_messagequeue mq;
  21. /* 消息队列中用到的放置消息的内存池 */
  22. static rt_uint8_t msg_pool[2048];
  23. ALIGN(RT_ALIGN_SIZE)
  24. static char thread1_stack[1024];
  25. static struct rt_thread thread1;
  26. /* 线程1入口函数 */
  27. static void thread1_entry(void *parameter)
  28. {
  29. char buf = 0;
  30. rt_uint8_t cnt = 0;
  31. while (1)
  32. {
  33. /* 从消息队列中接收消息 */
  34. if (rt_mq_recv(&mq, &buf, sizeof(buf), RT_WAITING_FOREVER) == RT_EOK)
  35. {
  36. rt_kprintf("thread1: recv msg from msg queue, the content:%c\n", buf);
  37. if (cnt == 19)
  38. {
  39. break;
  40. }
  41. }
  42. /* 延时50ms */
  43. cnt++;
  44. rt_thread_mdelay(50);
  45. }
  46. rt_kprintf("thread1: detach mq \n");
  47. rt_mq_detach(&mq);
  48. }
  49. ALIGN(RT_ALIGN_SIZE)
  50. static char thread2_stack[1024];
  51. static struct rt_thread thread2;
  52. /* 线程2入口 */
  53. static void thread2_entry(void *parameter)
  54. {
  55. int result;
  56. char buf = 'A';
  57. rt_uint8_t cnt = 0;
  58. while (1)
  59. {
  60. if (cnt == 8)
  61. {
  62. /* 发送紧急消息到消息队列中 */
  63. result = rt_mq_urgent(&mq, &buf, 1);
  64. if (result != RT_EOK)
  65. {
  66. rt_kprintf("rt_mq_urgent ERR\n");
  67. }
  68. else
  69. {
  70. rt_kprintf("thread2: send urgent message - %c\n", buf);
  71. }
  72. }
  73. else if (cnt >= 20)/* 发送20次消息之后退出 */
  74. {
  75. rt_kprintf("message queue stop send, thread2 quit\n");
  76. break;
  77. }
  78. else
  79. {
  80. /* 发送消息到消息队列中 */
  81. result = rt_mq_send(&mq, &buf, 1);
  82. if (result != RT_EOK)
  83. {
  84. rt_kprintf("rt_mq_send ERR\n");
  85. }
  86. rt_kprintf("thread2: send message - %c\n", buf);
  87. }
  88. buf++;
  89. cnt++;
  90. /* 延时5ms */
  91. rt_thread_mdelay(5);
  92. }
  93. }
  94. /* 消息队列示例的初始化 */
  95. int msgq_sample(void)
  96. {
  97. rt_err_t result;
  98. /* 初始化消息队列 */
  99. result = rt_mq_init(&mq,
  100. "mqt",
  101. &msg_pool[0], /* 内存池指向msg_pool */
  102. 1, /* 每个消息的大小是 1 字节 */
  103. sizeof(msg_pool), /* 内存池的大小是msg_pool的大小 */
  104. RT_IPC_FLAG_PRIO); /* 如果有多个线程等待,按照先来先得到的方法分配消息 */
  105. if (result != RT_EOK)
  106. {
  107. rt_kprintf("init message queue failed.\n");
  108. return -1;
  109. }
  110. rt_thread_init(&thread1,
  111. "thread1",
  112. thread1_entry,
  113. RT_NULL,
  114. &thread1_stack[0],
  115. sizeof(thread1_stack),
  116. THREAD_PRIORITY, THREAD_TIMESLICE);
  117. rt_thread_startup(&thread1);
  118. rt_thread_init(&thread2,
  119. "thread2",
  120. thread2_entry,
  121. RT_NULL,
  122. &thread2_stack[0],
  123. sizeof(thread2_stack),
  124. THREAD_PRIORITY, THREAD_TIMESLICE);
  125. rt_thread_startup(&thread2);
  126. return 0;
  127. }
  128. /* 导出到 msh 命令列表中 */
  129. MSH_CMD_EXPORT(msgq_sample, msgq sample);