msgq_sample.c 3.7 KB

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