Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
rt_mq_消息队列_msg_queue
消息队列例程一些小疑问?
发布于 2023-04-24 15:10:44 浏览:776
订阅该版
这段代码是官方消息队列代码(我自己修改一点)。 在创建消息队列中,我没有使用结构体struct messageequeue,而是使用rt_mq_t。自然我需要为指向对象分配内存,如果我使用malloc运行结果正常,使用rt_malloc没有运行结果。 ```c #include
#include
#include
#define STACK_SIZE 512 #define PRIORITY 25 #define TICK 20 #define MAXSEM 5 int main(void) { return 0; } /* 指向线程控制块的指针 */ static rt_thread_t tid1 = RT_NULL; static rt_thread_t tid2 = RT_NULL; static rt_mq_t mq; static rt_uint8_t msg_pool[2048]; #define THREAD_PRIORITY 10 #define THREAD_STACK_SIZE 512 #define THREAD_TIMESLICE 5 /* 线程 1 入口 */ static void thread1_entry(void *parameter) { char buf = 0; int cnt = 0; while(1){ if(rt_mq_recv(mq,&buf,1,RT_WAITING_FOREVER) == RT_EOK) { rt_kprintf("thread1: recv msg from msg queue,the content:%c\n",buf); if(cnt == 19) break; } cnt++; rt_thread_mdelay(50); } rt_kprintf("thread1: detch\n"); rt_mq_detach(mq); } /* 线程 2 入口 */ static void thread2_entry(void *parameter) { int result; char buf = 'a'; int cnt = 0; while(1) { if(cnt == 8) { result = rt_mq_urgent(mq,&buf,1); if(result != RT_EOK){ rt_kprintf("ERR\n"); }else{ rt_kprintf("thread2: send urgent message - %c\n",buf); } }else if(cnt >= 20){ rt_kprintf("quit!\n"); break; }else { result = rt_mq_send(mq,&buf,1); if(result != RT_EOK) { rt_kprintf("ERR\n"); } rt_kprintf("thread2: send message - %c\n",buf); } buf++; cnt++; rt_thread_mdelay(10); } } // rt_malloc int mqt(void) { rt_kprintf("mq\n"); mq = (rt_mq_t)rt_malloc(sizeof(48)); rt_err_t result = rt_mq_init(mq,"mq",msg_pool,1,sizeof(msg_pool),RT_IPC_FLAG_FIFO); // i 指的是message size 为 1 if(result != RT_EOK) //RT_EOK 为成功 { rt_kprintf("init message failed\n"); return -1; } /* 创建线程 1 */ tid1 = rt_thread_create("thread1", thread1_entry, RT_NULL, THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE); if (tid1 != RT_NULL) rt_thread_startup(tid1); /* 创建线程 2 */ tid2 = rt_thread_create("thread2", thread2_entry, RT_NULL, THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE); if (tid2 != RT_NULL) rt_thread_startup(tid2); return 0; } /* 导出到 msh 命令列表中 */ MSH_CMD_EXPORT(mqt, prio_inversion sample); ``` ![rt_malloc实验现象.png](https://oss-club.rt-thread.org/uploads/20230424/93a40eaddeb1940e6c33041b2db4fbd4.png)
查看更多
lchnu
2023-04-25
Witness, Understand, Skill
**1.sizeof(48)用错了** 你的代码等价于 ```c mq = (rt_mq_t)rt_malloc(4); ``` 你非要使用init静态方式创建mq的话,建议使用如下语句: ```c mq = (rt_mq_t)malloc(sizeof(struct rt_messagequeue)); ``` 在RT-Thread 4.0.2版本中,sizeof(struct rt_messagequeue)是60字节,不是48字节。不确定你的版本。 **2.具体原因分析** 你使用rt_malloc为mq分配了4个字节,但是在rt_mq_init对mq指向的内存进行了写入,破坏了RT-Thread的Heap数据结构,导致后续在rt_thread_create过程中,rt_malloc失败,你逐行debug就可以发现线程中分配struct rt_thread内存时,返回的thread为RT_NULL,线程没有创建成功。 至于你说用malloc(sizeof(48))能成功验证,我持怀疑态度。 不清楚你使用的IDE工具和平台,至少在RT-Thread Studio中,在使能了RT_USING_HEAP前提下,malloc和rt_malloc是一样的。
2
个回答
默认排序
按发布时间排序
月桂树GG_lwg
2023-04-24
这家伙很懒,什么也没写!
rt_malloc(sizeof(48))看起来怪怪的,你是想申请多大字节内存,48字节还是4字节?进入mem.c看下rt_malloc的源码,rt_malloc有最小字节数量的,你这应该是在rt_malloc里就报错了,单步跟进去看下具体是在哪个位置报错的。
撰写答案
登录
注册新账号
关注者
0
被浏览
776
关于作者
synrm
这家伙很懒,什么也没写!
提问
3
回答
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
RTT 源码分析笔记——互斥量篇
2
[E/app.filesystem] SD card mount to '/sdcard' failed!
3
单片机也能聊天?RT-Thread上跑通大语言模型
4
【RT-Thread】【ci】【scons】将ci.attachconfig.yml和scons结合使用
5
Rt-thread中OTA下载后,bootloader不搬程序
热门标签
RT-Thread Studio
串口
Env
LWIP
SPI
AT
Bootloader
Hardfault
CAN总线
FinSH
ART-Pi
DMA
USB
文件系统
RT-Thread
SCons
RT-Thread Nano
线程
MQTT
STM32
RTC
FAL
rt-smart
I2C_IIC
ESP8266
UART
WIZnet_W5500
ota在线升级
cubemx
PWM
flash
freemodbus
BSP
packages_软件包
潘多拉开发板_Pandora
定时器
ADC
flashDB
GD32
socket
编译报错
中断
Debug
rt_mq_消息队列_msg_queue
SFUD
msh
keil_MDK
ulog
C++_cpp
MicroPython
本月问答贡献
出出啊
1518
个答案
343
次被采纳
小小李sunny
1444
个答案
290
次被采纳
张世争
813
个答案
177
次被采纳
crystal266
549
个答案
161
次被采纳
whj467467222
1222
个答案
149
次被采纳
本月文章贡献
出出啊
1
篇文章
3
次点赞
小小李sunny
1
篇文章
1
次点赞
张世争
1
篇文章
3
次点赞
crystal266
2
篇文章
2
次点赞
whj467467222
2
篇文章
2
次点赞
回到
顶部
发布
问题
分享
好友
手机
浏览
扫码手机浏览
投诉
建议
回到
底部