Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
邮箱_mailbox
关于例程中邮箱相关问题
发布于 2021-07-07 23:41:42 浏览:813
订阅该版
```c /* * Copyright (c) 2006-2018, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes * 2018-08-24 yangjie the first version */ /* * 程序清单:邮箱例程 * * 这个程序会创建2个动态线程,一个静态的邮箱对象,其中一个线程往邮箱中发送邮件, * 一个线程往邮箱中收取邮件。 */ #include
#define THREAD_PRIORITY 10 #define THREAD_TIMESLICE 5 /* 邮箱控制块 */ static struct rt_mailbox mb; /* 用于放邮件的内存池 */ static char mb_pool[128]; static char mb_str1[] = "I'm a mail!"; static char mb_str2[] = "this is another mail!"; static char mb_str3[] = "over"; ALIGN(RT_ALIGN_SIZE) static char thread1_stack[1024]; static struct rt_thread thread1; /* 线程1入口 */ static void thread1_entry(void *parameter) { char *str; while (1) { rt_kprintf("thread1: try to recv a mail\n"); /* 从邮箱中收取邮件 */ if (rt_mb_recv(&mb, (rt_uint32_t *)&str, RT_WAITING_FOREVER) == RT_EOK) { rt_kprintf("thread1: get a mail from mailbox, the content:%s\n", str); rt_kprintf("entry is %d,in_offset is %d,out_offset if %d\n",mb.entry,mb.in_offset,mb.out_offset); if (str == mb_str3) break; /* 延时100ms */ rt_thread_mdelay(300); } } /* 执行邮箱对象脱离 */ rt_mb_detach(&mb); } ALIGN(RT_ALIGN_SIZE) static char thread2_stack[1024]; static struct rt_thread thread2; /* 线程2入口 */ static void thread2_entry(void *parameter) { rt_uint8_t count; count = 0; while (count < 20) { count ++; if (count & 0x1) { /* 发送mb_str1地址到邮箱中 */ rt_mb_send(&mb, (rt_uint32_t)&mb_str1); } else { /* 发送mb_str2地址到邮箱中 */ rt_mb_send(&mb, (rt_uint32_t)&mb_str2); } rt_kprintf("entry is %d,in_offset is %d,out_offset if %d\n",mb.entry,mb.in_offset,mb.out_offset); /* 延时200ms */ rt_thread_mdelay(20); } /* 发送邮件告诉线程1,线程2已经运行结束 */ rt_mb_send(&mb, (rt_uint32_t)&mb_str3); } int mailbox_sample(void) { rt_err_t result; /* 初始化一个mailbox */ result = rt_mb_init(&mb, "mbt", /* 名称是mbt */ &mb_pool[0], /* 邮箱用到的内存池是mb_pool */ sizeof(mb_pool) / 4, /* 邮箱中的邮件数目,因为一封邮件占4字节 */ RT_IPC_FLAG_FIFO); /* 采用FIFO方式进行线程等待 */ if (result != RT_EOK) { rt_kprintf("init mailbox failed.\n"); return -1; } rt_thread_init(&thread1, "thread1", thread1_entry, RT_NULL, &thread1_stack[0], sizeof(thread1_stack), THREAD_PRIORITY, THREAD_TIMESLICE); rt_thread_startup(&thread1); rt_thread_init(&thread2, "thread2", thread2_entry, RT_NULL, &thread2_stack[0], sizeof(thread2_stack), THREAD_PRIORITY, THREAD_TIMESLICE); rt_thread_startup(&thread2); return 0; } /* 导出到 msh 命令列表中 */ MSH_CMD_EXPORT(mailbox_sample, mailbox sample); ``` ``` \ | / - RT - Thread Operating System / | \ 3.1.0 build Jul 7 2021 2006 - 2018 Copyright by rt-thread team msh >mai mailbox_sample msh >mailbox_sample thread1: try to recv a mail entry is 1,in_offset is 1,out_offset if 0 thread1: get a mail from mailbox, the content:I'm a mail! entry is 0,in_offset is 1,out_offset if 1 msh >entry is 1,in_offset is 2,out_offset if 1 entry is 2,in_offset is 3,out_offset if 1 entry is 3,in_offset is 4,out_offset if 1 entry is 4,in_offset is 5,out_offset if 1 entry is 5,in_offset is 6,out_offset if 1 entry is 6,in_offset is 7,out_offset if 1 entry is 7,in_offset is 8,out_offset if 1 entry is 8,in_offset is 9,out_offset if 1 entry is 9,in_offset is 10,out_offset if 1 entry is 10,in_offset is 11,out_offset if 1 entry is 11,in_offset is 12,out_offset if 1 entry is 12,in_offset is 13,out_offset if 1 entry is 13,in_offset is 14,out_offset if 1 entry is 14,in_offset is 15,out_offset if 1 entry is 15,in_offset is 16,out_offset if 1 thread1: try to recv a mail thread1: get a mail from mailbox, the content:this is another mail! entry is 14,in_offset is 16,out_offset if 2 entry is 15,in_offset is 17,out_offset if 2 entry is 16,in_offset is 18,out_offset if 2 entry is 17,in_offset is 19,out_offset if 2 entry is 18,in_offset is 20,out_offset if 2 thread1: try to recv a mail thread1: get a mail from mailbox, the content:I'm a mail! entry is 18,in_offset is 21,out_offset if 3 thread1: try to recv a mail thread1: get a mail from mailbox, the content:this is another mail! entry is 17,in_offset is 21,out_offset if 4 thread1: try to recv a mail thread1: get a mail from mailbox, the content:I'm a mail! entry is 16,in_offset is 21,out_offset if 5 thread1: try to recv a mail thread1: get a mail from mailbox, the content:this is another mail! entry is 15,in_offset is 21,out_offset if 6 thread1: try to recv a mail thread1: get a mail from mailbox, the content:I'm a mail! entry is 14,in_offset is 21,out_offset if 7 thread1: try to recv a mail thread1: get a mail from mailbox, the content:this is another mail! entry is 13,in_offset is 21,out_offset if 8 thread1: try to recv a mail thread1: get a mail from mailbox, the content:I'm a mail! entry is 12,in_offset is 21,out_offset if 9 thread1: try to recv a mail thread1: get a mail from mailbox, the content:this is another mail! entry is 11,in_offset is 21,out_offset if 10 thread1: try to recv a mail thread1: get a mail from mailbox, the content:I'm a mail! entry is 10,in_offset is 21,out_offset if 11 thread1: try to recv a mail thread1: get a mail from mailbox, the content:this is another mail! entry is 9,in_offset is 21,out_offset if 12 thread1: try to recv a mail thread1: get a mail from mailbox, the content:I'm a mail! entry is 8,in_offset is 21,out_offset if 13 thread1: try to recv a mail thread1: get a mail from mailbox, the content:this is another mail! entry is 7,in_offset is 21,out_offset if 14 thread1: try to recv a mail thread1: get a mail from mailbox, the content:I'm a mail! entry is 6,in_offset is 21,out_offset if 15 thread1: try to recv a mail thread1: get a mail from mailbox, the content:this is another mail! entry is 5,in_offset is 21,out_offset if 16 thread1: try to recv a mail thread1: get a mail from mailbox, the content:I'm a mail! entry is 4,in_offset is 21,out_offset if 17 thread1: try to recv a mail thread1: get a mail from mailbox, the content:this is another mail! entry is 3,in_offset is 21,out_offset if 18 thread1: try to recv a mail thread1: get a mail from mailbox, the content:I'm a mail! entry is 2,in_offset is 21,out_offset if 19 thread1: try to recv a mail thread1: get a mail from mailbox, the content:this is another mail! entry is 1,in_offset is 21,out_offset if 20 thread1: try to recv a mail thread1: get a mail from mailbox, the content:over entry is 0,in_offset is 21,out_offset if 21 ``` 以上是我将例程的mailbox_sample进行了修改,对于输出,我有以下几个疑问: 1.邮件的发送和收取的对应关系,比如我按顺序发送了1,2,3封邮件,然后进行接收,接收顺序是1,2,3吗? 2.控制块里的entry,in_offset,out_offset的物理意义是什么?in_offset,out_offset为什么会一直增加?会不会清零? 3.如果发送邮件超出预先定义的内存大小会发生什么情况?
查看更多
123
认证专家
2021-07-08
这家伙很懒,什么也没写!
@刘启锋 你问的问题,自己试一下不就知道了,有源码有教程有测试用例,还能在线仿真。你就缺一个实际行动。
3
个回答
默认排序
按发布时间排序
出出啊
2021-07-08
恃人不如自恃,人之为己者不如己之自为也
看in_offset,out_offset 两个值的变化,跟消息队列一样,有个队列,应该会溢出的,mb_pool 定义 128 字节,应该有32封就满了。 in out 的差值就是有多少没拆封的邮件。相等就没有邮件。
游走在01的海洋
2021-07-08
In the end, it's not the years in your life that count. It's the life in your years.
我回答下1、3问 一、问题1;接收顺序时按顺序来接收的;但是线程如何接收就是根据定义邮箱是的方式来看了;如果是RT_IPC_FLAG_FIFO,那就是就绪线程先来先拿;如果时RT_IPC_FLAG_PRIO,就绪线程优先级高的先拿; 二、问题3;如果邮箱满了,设置的超时时间到了也没有发送出去,就会报错,可以在这里做一个错误处理; ![1.JPG](https://oss-club.rt-thread.org/uploads/20210708/d5ff06436ff07bb82f0eb6f499e7999b.jpg.webp)具体可以查看邮箱手册
撰写答案
登录
注册新账号
关注者
0
被浏览
813
关于作者
刘启锋
这家伙很懒,什么也没写!
提问
9
回答
2
被采纳
0
关注TA
发私信
相关问题
1
邮箱阻塞接收设为永久阻塞却收到了RT_ETIMEOUT错误码
2
mailbox 邮件收发只能在一个文件中进行
3
mailbox接收函数对于超时时间的处理疑问
4
关于事件集和邮箱那一块
5
邮箱能否支持多个收听?
6
关于邮箱传递消息,当消息很长的时候
7
一对多线程间通信的问题
8
邮箱,队列,信号在LCD菜单中内容刷新的选用
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
vscode插件 - RT-Thread Studio项目助手 &amp; 跨平台开发
2
Console串口使用说明
3
WATCHDOG设备驱动开发
4
【NXP-MCXA153】eFlexPWM驱动移植
5
RT-Thread 下 Ethernet/IP 的支持
热门标签
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
UART
WIZnet_W5500
ota在线升级
PWM
cubemx
freemodbus
flash
packages_软件包
BSP
潘多拉开发板_Pandora
定时器
ADC
GD32
flashDB
socket
中断
编译报错
Debug
rt_mq_消息队列_msg_queue
SFUD
keil_MDK
msh
ulog
MicroPython
C++_cpp
本月问答贡献
出出啊
1517
个答案
342
次被采纳
小小李sunny
1444
个答案
290
次被采纳
张世争
812
个答案
177
次被采纳
crystal266
547
个答案
161
次被采纳
whj467467222
1222
个答案
148
次被采纳
本月文章贡献
出出啊
1
篇文章
2
次点赞
小小李sunny
1
篇文章
1
次点赞
张世争
1
篇文章
2
次点赞
crystal266
2
篇文章
2
次点赞
whj467467222
2
篇文章
2
次点赞
回到
顶部
发布
问题
分享
好友
手机
浏览
扫码手机浏览
投诉
建议
回到
底部