Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
rt_mq_消息队列_msg_queue
邮箱_mailbox
rt_thread_mdelay
阅读官方的邮箱与消息队列例程时没有弄懂延时关系
发布于 2022-08-22 15:00:31 浏览:511
订阅该版
在官方的消息队列例程中,用来发送消息的线程2每次循环延时5ms,接收消息的线程1延时50ms,为什么输出语句中显示线程2发送5次后线程1就结束挂起状态实现一次接收呢? ```c #include
/* 消息队列控制块 */ static struct rt_messagequeue mq; /* 消息队列中用到的放置消息的内存池 */ static rt_uint8_t msg_pool[2048]; ALIGN(RT_ALIGN_SIZE) static char thread1_stack[1024]; static struct rt_thread thread1; /* 线程 1 入口函数 */ static void thread1_entry(void *parameter) { char buf = 0; rt_uint8_t cnt = 0; while (1) { /* 从消息队列中接收消息 */ if (rt_mq_recv(&mq, &buf, sizeof(buf), RT_WAITING_FOREVER) == RT_EOK) { rt_kprintf("thread1: recv msg from msg queue, the content:%c\n", buf); if (cnt == 19) { break; } } /* 延时 50ms */ cnt++; rt_thread_mdelay(50); } rt_kprintf("thread1: detach mq \n"); rt_mq_detach(&mq); } ALIGN(RT_ALIGN_SIZE) static char thread2_stack[1024]; static struct rt_thread thread2; /* 线程 2 入口 */ static void thread2_entry(void *parameter) { int result; char buf = 'A'; rt_uint8_t cnt = 0; while (1) { if (cnt == 8) { /* 发送紧急消息到消息队列中 */ result = rt_mq_urgent(&mq, &buf, 1); if (result != RT_EOK) { rt_kprintf("rt_mq_urgent ERR\n"); } else { rt_kprintf("thread2: send urgent message - %c\n", buf); } } else if (cnt>= 20)/* 发送 20 次消息之后退出 */ { rt_kprintf("message queue stop send, thread2 quit\n"); break; } else { /* 发送消息到消息队列中 */ result = rt_mq_send(&mq, &buf, 1); if (result != RT_EOK) { rt_kprintf("rt_mq_send ERR\n"); } rt_kprintf("thread2: send message - %c\n", buf); } buf++; cnt++; /* 延时 5ms */ rt_thread_mdelay(5); } } /* 消息队列示例的初始化 */ int msgq_sample(void) { rt_err_t result; /* 初始化消息队列 */ result = rt_mq_init(&mq, "mqt", &msg_pool[0], /* 内存池指向 msg_pool */ 1, /* 每个消息的大小是 1 字节 */ sizeof(msg_pool), /* 内存池的大小是 msg_pool 的大小 */ RT_IPC_FLAG_PRIO); /* 如果有多个线程等待,优先级大小的方法分配消息 */ if (result != RT_EOK) { rt_kprintf("init message queue failed.\n"); return -1; } rt_thread_init(&thread1, "thread1", thread1_entry, RT_NULL, &thread1_stack[0], sizeof(thread1_stack), 25, 5); rt_thread_startup(&thread1); rt_thread_init(&thread2, "thread2", thread2_entry, RT_NULL, &thread2_stack[0], sizeof(thread2_stack), 25, 5); rt_thread_startup(&thread2); return 0; } /* 导出到 msh 命令列表中 */ MSH_CMD_EXPORT(msgq_sample, msgq sample); ``` 仿真结果: ```c \ | / - RT - Thread Operating System / | \ 3.1.0 build Aug 24 2018 2006 - 2018 Copyright by rt-thread team msh > msgq_sample msh >thread2: send message - A thread1: recv msg from msg queue, the content:A thread2: send message - B thread2: send message - C thread2: send message - D thread2: send message - E thread1: recv msg from msg queue, the content:B thread2: send message - F thread2: send message - G thread2: send message - H thread2: send urgent message - I thread2: send message - J thread1: recv msg from msg queue, the content:I thread2: send message - K thread2: send message - L thread2: send message - M thread2: send message - N thread2: send message - O thread1: recv msg from msg queue, the content:C thread2: send message - P thread2: send message - Q thread2: send message - R thread2: send message - S thread2: send message - T thread1: recv msg from msg queue, the content:D message queue stop send, thread2 quit thread1: recv msg from msg queue, the content:E thread1: recv msg from msg queue, the content:F thread1: recv msg from msg queue, the content:G … thread1: recv msg from msg queue, the content:T thread1: detach mq ```
查看更多
1
个回答
默认排序
按发布时间排序
sync
2022-08-22
这家伙很懒,什么也没写!
我实际测试了下,F407的板子 发送和接收中加入了rt_tick_get的打印 线程2里面的rt_thread_mdelay(5) 时间是不准确的 实际结果如下: ```C \ | / - RT - Thread Operating System / | \ 4.0.2 build Sep 1 2021 2006 - 2019 Copyright by rt-thread team msh > msh > msh >ms msgq_sample msh >msgq_sample msh >thread2: send message - A 4557 thread1: recv msg from msg queue, the contthread2: send messageent:A 4561 - B 4565 thread2: send message - C 4574 thread2: send message - D 4582 thread2: send message - E 4590 thread2: send message - F 4598 thread2: send message - G 4606 thread2: send message - H 4614 thread1: recv msg from msg queue, the contthread2: seent:B 4618 nd urgent message - I 4622 thread2: send message - J 4631 thread2: send message - K 4639 thread2: send message - L 4647 thread2: send message - M 4655 thread2: send message - N 4663 thread2: send message - O 4671 thread1: recv msg from msg queue, the content:I 4674 thread2: send message - P 4683 thread2: send message - Q 4691 thread2: send message - R 4699 thread2: send message - S 4707 thread2: send message - T 4715 message queue stop send, thread2 quit thread1: recv msg from msg queue, the content:C 4729 thread1: recv msg from msg queue, the content:D 4784 thread1: recv msg from msg queue, the content:E 4839 thread1: recv msg from msg queue, the content:F 4894 thread1: recv msg from msg queue, the content:G 4949 thread1: recv msg from msg queue, the content:H 5004 thread1: recv msg from msg queue, the content:J 5059 thread1: recv msg from msg queue, the content:K 5114 thread1: recv msg from msg queue, the content:L 5169 thread1: recv msg from msg queue, the content:M 5224 thread1: recv msg from msg queue, the content:N 5279 thread1: recv msg from msg queue, the content:O 5334 thread1: recv msg from msg queue, the content:P 5389 thread1: recv msg from msg queue, the content:Q 5444 thread1: recv msg from msg queue, the content:R 5499 thread1: recv msg from msg queue, the content:S 5554 thread1: recv msg from msg queue, the content:T 5609 thread1: detach mq msh >ps thread pri status sp stack size max used left tick error -------- --- ------- ---------- ---------- ------ ---------- --- tshell 20 running 0x000000d0 0x00001000 15% 0x00000006 000 tidle0 31 ready 0x0000005c 0x00000100 51% 0x00000015 000 timer 4 suspend 0x0000007c 0x00000200 24% 0x00000009 000 main 10 suspend 0x000000bc 0x00000800 14% 0x00000014 000 ```
撰写答案
登录
注册新账号
关注者
0
被浏览
511
关于作者
planetx
这家伙很懒,什么也没写!
提问
1
回答
0
被采纳
0
关注TA
发私信
相关问题
1
rt_object_init中报assertion failed错误?
2
在 MDK中的NANO 里创建消息队列失败,内存堆已开启
3
如何用消息队列传递结构体数据
4
消息队列满了以后接收乱码
5
消息队列传输不定长数据
6
使用消息队列在线程中发送总失败
7
初始化第二个消息队列时发生硬件错误
8
rtthread消息队列一对多的情况
9
消息队列为什么会出现获取到的内容有旧的数据?
10
通过消息队列名称,获取消息队列句柄
推荐文章
1
RT-Thread应用项目汇总
2
玩转RT-Thread系列教程
3
国产MCU移植系列教程汇总,欢迎查看!
4
机器人操作系统 (ROS2) 和 RT-Thread 通信
5
五分钟玩转RT-Thread新社区
6
【技术三千问】之《玩转ART-Pi》,看这篇就够了!干货汇总
7
关于STM32H7开发板上使用SDIO接口驱动SD卡挂载文件系统的问题总结
8
STM32的“GPU”——DMA2D实例详解
9
RT-Thread隐藏的宝藏之completion
10
【ART-PI】RT-Thread 开启RTC 与 Alarm组件
最新文章
1
使用百度AI助手辅助编写一个rt-thread下的ONVIF设备发现功能的功能代码
2
RT-Thread 发布 EtherKit开源以太网硬件!
3
rt-thread使用cherryusb实现虚拟串口
4
《C++20 图形界面程序:速度与渲染效率的双重优化秘籍》
5
《原子操作:程序世界里的“最小魔法单位”解析》
热门标签
RT-Thread Studio
串口
Env
LWIP
SPI
AT
Bootloader
Hardfault
CAN总线
FinSH
ART-Pi
USB
DMA
文件系统
RT-Thread
SCons
RT-Thread Nano
线程
MQTT
STM32
RTC
FAL
rt-smart
ESP8266
I2C_IIC
WIZnet_W5500
ota在线升级
UART
PWM
cubemx
freemodbus
flash
packages_软件包
BSP
潘多拉开发板_Pandora
定时器
ADC
GD32
flashDB
socket
中断
Debug
编译报错
msh
SFUD
keil_MDK
rt_mq_消息队列_msg_queue
at_device
ulog
C++_cpp
本月问答贡献
踩姑娘的小蘑菇
7
个答案
3
次被采纳
张世争
8
个答案
2
次被采纳
rv666
5
个答案
2
次被采纳
用户名由3_15位
11
个答案
1
次被采纳
KunYi
6
个答案
1
次被采纳
本月文章贡献
程序员阿伟
6
篇文章
2
次点赞
hhart
3
篇文章
4
次点赞
大龄码农
1
篇文章
2
次点赞
ThinkCode
1
篇文章
1
次点赞
Betrayer
1
篇文章
1
次点赞
回到
顶部
发布
问题
分享
好友
手机
浏览
扫码手机浏览
投诉
建议
回到
底部