RTthread线程间通信(邮箱,消息队列,信号/软件中断)---01实际使用API函数

news2024/9/26 5:17:13

layout: post
title: “RT-Thread线程间通信”
date: 2024-2-5 15:39:08 +0800
tags: RT-Thread


线程间通信

这一篇是实际使用, 代码分析看后面的文章

一般可以使用全局变量以及线程间同步进行实现

RT-Thread也提供了一部分的通信机制

邮箱

一个线程发送, 另外的线程接受信息, 进行处理

使用邮箱的时候每一次只能发送一个四字节的数据(32位处理器),特点是开销比较低,效率较高

可以发送一个地址从而达到发送多个数据的目的

非阻塞方式的邮件发送过程能够安全的应用于中断服务中, 发送以及接受信息的时候可以使用阻塞的模式

邮箱有一个缓存区, 使用rt_mailbox_t进行控制

实际使用

image-20240205161002594

创建(初始化)
/** 动态的方式创建
 * This function will create a mailbox object from system resource
 *
 * @param name the name of mailbox 记录一个名字
 * @param size the size of mailbox 记录一下缓存区的大小
 * @param flag the flag of mailbox 一个标志位
 *
 * @return the created mailbox, RT_NULL on error happen
 */
rt_mailbox_t rt_mb_create(const char *name, rt_size_t size, rt_uint8_t flag)
 /** 静态的方式创建
 * This function will initialize a mailbox and put it under control of resource
 * management.
 *
 * @param mb the mailbox object 邮箱的句柄
 * @param name the name of mailbox 名字
 * @param msgpool the begin address of buffer to save received mail 缓存区的地址
 * @param size the size of mailbox	缓冲区大小
 * @param flag the flag of mailbox 标志
 *
 * @return the operation status, RT_EOK on successful
 */
rt_err_t rt_mb_init(rt_mailbox_t mb,
                    const char  *name,
                    void        *msgpool,
                    rt_size_t    size,
                    rt_uint8_t   flag)

这一个标志位可以为RT_IPC_FLAG_FIFO或RT_IPC_FLAG_PRIO, 设置的是挂起任务被释放的时候是按照进入的顺序先进入的先出去还是优先级比较高的先出去

删除
/**动态
 * This function will delete a mailbox object and release the memory
 *
 * @param mb the mailbox object
 *
 * @return the error code
 */
rt_err_t rt_mb_delete(rt_mailbox_t mb)
/**静态
 * This function will detach a mailbox from resource management
 *
 * @param mb the mailbox object
 *
 * @return the operation status, RT_EOK on successful
 */
rt_err_t rt_mb_detach(rt_mailbox_t mb)
发送邮件
/**
 * This function will send a mail to mailbox object, if there are threads
 * suspended on mailbox object, it will be waked up. This function will return
 * immediately, if you want blocking send, use rt_mb_send_wait instead.
 *
 * @param mb the mailbox object
 * @param value the mail 要发送的数据
 *
 * @return the error code
 */
rt_err_t rt_mb_send(rt_mailbox_t mb, rt_ubase_t value)

这是一个不等待的时钟发送函数

/**
 * This function will send a mail to mailbox object. If the mailbox is full,
 * current thread will be suspended until timeout.
 *
 * @param mb the mailbox object
 * @param value the mail
 * @param timeout the waiting time 多了一个等待时间
 *
 * @return the error code
 */
rt_err_t rt_mb_send_wait(rt_mailbox_t mb,
                         rt_ubase_t   value,
                         rt_int32_t   timeout)
接收
/**
 * This function will receive a mail from mailbox object, if there is no mail
 * in mailbox object, the thread shall wait for a specified time.
 *
 * @param mb the mailbox object
 * @param value the received mail will be saved in 给出一个存放收到的数据的位置
 * @param timeout the waiting time
 *
 * @return the error code
 */
rt_err_t rt_mb_recv(rt_mailbox_t mb, rt_ubase_t *value, rt_int32_t timeout)

使用技巧

可以使用一个这样的结构体, 每次发送这一个结构体的地址, 进行大于32字节的数据发送

struct msg
{
    uint32_t *data;
    uint32_t size;
}

消息队列

是邮箱的扩展, 没有4字节的限制

消息队列能够接收来自线程或中断服务例程中不固定长度的消息,并把消息缓存在自己的内存空间中。这些消息使用的是链表进行连接, 消息先进先出

这一个使用的拷贝的模式进行传输, 不建议直接发送大量数据(可以发送一个地址)

可以用于发送不定长的数据, 实际使用的时候可以使用消息队列发送消息, 使用邮箱表示接收到数据了

image-20240205164352685

实际使用

image-20240205164847142

创建
/**动态
 * This function will create a message queue object from system resource
 *
 * @param name the name of message queue 名字
 * @param msg_size the size of message	每一个消息的大小(字节)
 * @param max_msgs the maximum number of message in queue 记录一下消息的最大的个数
 * @param flag the flag of message queue 一个标志
 *
 * @return the created message queue, RT_NULL on error happen
 */
rt_mq_t rt_mq_create(const char *name,
                     rt_size_t   msg_size,
                     rt_size_t   max_msgs,
                     rt_uint8_t  flag)
/**静态
 * This function will initialize a message queue and put it under control of
 * resource management.
 *
 * @param mq the message object	对象的句柄
 * @param name the name of message queue
 * @param msgpool the beginning address of buffer to save messages 缓冲区的地址, 动态申请
 *		 的时候这个的大小是 (一个数据的大小+sizeof(struct rt_mq_message)) * mq->max_msgs
 * @param msg_size the maximum size of message一个消息的大小
 * @param pool_size the size of buffer to save messages 缓冲区的大小
 * @param flag the flag of message queue
 *
 * @return the operation status, RT_EOK on successful
 */
rt_err_t rt_mq_init(rt_mq_t     mq,
                    const char *name,
                    void       *msgpool,
                    rt_size_t   msg_size,
                    rt_size_t   pool_size,
                    rt_uint8_t  flag)

这一个标志位可以为RT_IPC_FLAG_FIFO或RT_IPC_FLAG_PRIO, 设置的是挂起任务被释放的时候是按照进入的顺序先进入的先出去还是优先级比较高的先出去

删除
/**动态
 * This function will delete a message queue object and release the memory
 *
 * @param mq the message queue object
 *
 * @return the error code
 */
rt_err_t rt_mq_delete(rt_mq_t mq)
/**静态
 * This function will detach a message queue object from resource management
 *
 * @param mq the message queue object
 *
 * @return the operation status, RT_EOK on successful
 */
rt_err_t rt_mq_detach(rt_mq_t mq)
发送消息
/**
 * This function will send a message to message queue object, if there are
 * threads suspended on message queue object, it will be waked up.
 *
 * @param mq the message queue object 
 * @param buffer the message 发送的消息的地址
 * @param size the size of buffer 发送的数据的大小
 *
 * @return the error code
 */
rt_err_t rt_mq_send(rt_mq_t mq, const void *buffer, rt_size_t size)

不等待

/**
 * This function will send a message to message queue object. If the message queue is full,
 * current thread will be suspended until timeout.
 *
 * @param mq the message queue object
 * @param buffer the message
 * @param size the size of buffer
 * @param timeout the waiting time
 *
 * @return the error code
 */
rt_err_t rt_mq_send_wait(rt_mq_t     mq,
                         const void *buffer,
                         rt_size_t   size,
                         rt_int32_t  timeout)

等待

/**
 * This function will send an urgent message to message queue object, which
 * means the message will be inserted to the head of message queue. If there
 * are threads suspended on message queue object, it will be waked up.
 *
 * @param mq the message queue object
 * @param buffer the message
 * @param size the size of buffer
 *
 * @return the error code
 */
rt_err_t rt_mq_urgent(rt_mq_t mq, const void *buffer, rt_size_t size)

发送一个紧急的消息, 这一个消息会直接放到队首

接收
/**
 * This function will receive a message from message queue object, if there is
 * no message in message queue object, the thread shall wait for a specified
 * time.
 *
 * @param mq the message queue object
 * @param buffer the received message will be saved in 接收到的数据
 * @param size the size of buffer
 * @param timeout the waiting time
 *
 * @return the error code
 */
rt_err_t rt_mq_recv(rt_mq_t    mq,
                    void      *buffer,
                    rt_size_t  size,
                    rt_int32_t timeout)

信号(软件中断信号)

**注: **信号这块应该是要在微内核里使用,如果你是用宏内核版本,不推荐使用信号功能。

POSIX标准定义了sigset_t类型来定义一个信号集, 实际是一个unsigned long类型的数据, 应用程序能够使用的信号为SIGUSR1(10)和SIGUSR2(12)

他的本质是一个软件中断

收到信号的线程实际的处理方法有三种

  • 类似中断的处理程序,对于需要处理的信号,线程可以指定处理函数,由该函数来处理。
  • 忽略某个信号,对该信号不做任何处理,就像未发生过一样。
  • 对该信号的处理保留系统的默认值。

image-20240205171441255

需要定义RT_USING_SIGNALS这一个宏

实际使用

image-20240205171459644

安装信号

如果线程要处理某一信号,那么就要在线程中安装该信号。

主要用来确定信号值及线程针对该信号值的动作之间的映射关系,即线程将要处理哪个信号,该信号被传递给线程时,将执行何种操作。

rt_sighandler_t rt_signal_install(int signo, rt_sighandler_t handler)

(这一个函数是给现在的线程安装)

signo信号值(只有SIGUSR1和SIGUSR2是开放给用户使用的)

handler设置对信号值的处理方式, 这一个的实际的函数是void (*rt_sighandler_t)(int signo);

也可以使用SIG_IGN,忽略某个信号, SIG_DFL,系统会调用默认的处理函数_signal_default_handler()

返回安装信号前的handler值表示成功

阻塞(屏蔽)信号

该信号将不会递达给安装此信号的线程,也不会引发软中断处理。

void rt_signal_mask(int signo)
解除信号阻塞
void rt_signal_unmask(int signo)
发送信号
int rt_thread_kill(rt_thread_t tid, int sig)

tid: 接收信号的线程

sig: 信号值

等待信号
int rt_signal_wait(const rt_sigset_t *set, rt_siginfo_t *si, rt_int32_t timeout)

set: 指定等待的信号

si: 指向存储等到信号信息的指针

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1438740.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

二叉树的最近公共祖先,力扣

目录 题目地址: 题目: 审题目事例提示: 解题方法: 解题分析: 解题思路: 代码实现: 补充说明: 代码优化: 代码实现(存储父节点) : 题目地址: 236. 二叉树的最近…

人工智能的运维服务有哪些?智能化运维发展方向有哪些

当前人工智能运维服务的主要类型和特点?智能化运维的未来发展趋势,包括但不限于技术进步、行业应用以及潜在的挑战和机遇。 目前人工智能运维服务的主要类型和优点包括: 基于规则的专家系统:根据事先定义的规则和逻辑进行故障检测和处理。特…

linux centos 安装teleport

效果 安装 1.创建目录 mkdir -p /opt/teleport/data cd /opt/teleport/2.下载解压文件 wget https://tp4a.com/static/download/teleport-server-linux-x64-3.6.4-b3.tar.gz tar -xvf teleport-server-linux-x64-3.6.4-b3.tar.gz3.安装 cd /opt/teleport/teleport-server-l…

每日一题来啦!请查收~(至少是其他数字两倍,两个数组的交集)

今天要写的题目有哪些呢&#xff1f; 747. 至少是其他数字两倍的最大数 - 力扣&#xff08;LeetCode&#xff09; int dominantIndex(int* nums, int numsSize) {int max0;maxnums[0];int index0;for(int i1;i<numsSize;i){if(nums[i]>max){maxnums[i];indexi;i;//找出最…

c++之说_12|模板

关于模板&#xff0c;至少我们要先了解几个概念 一&#xff1a;函数模板 二&#xff1a;类模板 三&#xff1a;模板特化 四&#xff1a;形参参数包 模板覆盖的东西太多 我目前也不了解太多 函数模板 语法 template<typename 类型名,typename 类型名,typename ...多参…

KMP算法|next指针|详细讲解学习

KMP 算法介绍 KMP 算法是基于串的朴素模式匹配算法优化的。 串的朴素模式匹配算法是将主串中所有的与模式串长度相等的子串与模式串进行比较&#xff0c;如果模式串与进行比较的的子串相等&#xff0c;就匹配成功&#xff0c;否则匹配失败。 在 KMP 算法的理解的基础上&#x…

SpringFramework实战指南(六)

SpringFramework实战指南(六) 4.4 基于 配置类 方式管理 Bean4.4.1 完全注解开发理解4.4.2 实验一:配置类和扫描注解4.4.3 实验二:@Bean定义组件4.4.4 实验三:高级特性:@Bean注解细节4.4.5 实验四:高级特性:@Import扩展4.4.6 实验五:基于注解+配置类方式整合三层架构组…

C#(C Sharp)学习笔记_前言及Visual Studio Code配置C#运行环境【一】

前言 这可以说是我第一次正式的踏入C#的学习道路&#xff0c;我真没想过我两年前是怎么跳过C#去学Unity3D游戏开发的&#xff08;当然了&#xff0c;游戏开发肯定是没有成功的&#xff0c;都是照搬代码&#xff09;。而现在&#xff0c;我真正地学习一下C#&#xff0c;就和去年…

调和平均

L1-4 调和平均 分数 10 作者 陈越 单位 浙江大学 N 个正数的算数平均是这些数的和除以 N&#xff0c;它们的调和平均是它们倒数的算数平均的倒数。本题就请你计算给定的一系列正数的调和平均值。 输入格式&#xff1a…

Springboot 整合 Elasticsearch(三):使用RestHighLevelClient操作ES ①

&#x1f4c1; 前情提要&#xff1a; Springboot 整合 Elasticsearch&#xff08;一&#xff09;&#xff1a;Linux下安装 Elasticsearch 8.x Springboot 整合 Elasticsearch&#xff08;二&#xff09;&#xff1a;使用HTTP请求来操作ES 目录 一、Springboot 整合 Elasticsea…

Pymysql之Connection中常用API

Connection中常用API 1、open() &#xff1a;检测数据库是否连接。 connect.open&#xff1a;如果数据库连接返回Trhe&#xff0c;否则返回False。 2、ping(reconnectTrue) connect.ping(reconnectTrue):如果reconnectTrue表示连接断开后&#xff0c;重新进行连接。 import…

Android Compose 一个音视频APP——Magic Music Player

Magic Music APP Magic Music APP Magic Music APP概述效果预览-视频资源功能预览Library歌曲播放效果预览歌曲播放依赖注入设置播放源播放进度上一首&下一首UI响应 歌词歌词解析解析成行逐行解析 视频播放AndroidView引入Exoplayer自定义Exoplayer样式横竖屏切换 歌曲多任…

Vue中使用 Element-ui form和 el-dialog 进行自定义表单校验清除表单状态

文章目录 问题分析 问题 在使用 Element-ui el-form 和 el-dialog 进行自定义表单校验时&#xff0c;出现点击编辑按钮之后再带年纪新增按钮&#xff0c;出现如下情况&#xff0c;新增弹出表单进行了一次表单验证&#xff0c;而这时不应该要表单验证的 分析 在寻找多种解决…

Google DeepMind最新研究,将视觉语言大模型作为强化学习的全新奖励来源

论文题目&#xff1a;Vision-Language Models as a Source of Rewards 论文链接&#xff1a;https://arxiv.org/abs/2312.09187 在大型语言模型&#xff08;LLM&#xff09;不断发展的进程中&#xff0c;强化学习扮演了重要的角色&#xff0c;ChatGPT就是在GPT-3.5的基础上经过…

Stable Diffusion 模型下载:RealCartoon-Pixar - V8

文章目录 模型介绍生成案例案例一案例二案例三案例四案例五案例六案例七案例八案例九案例十下载地址模型介绍 这个检查点是从 RealCartoon3D 检查点分支出来的。它的目标是在整体上产生更多的“皮克斯”风格。我非常喜欢3D卡通的外观,希望能够创建出具有

Linux死机排查方法——内存日志

一般情况下&#xff0c;Linux系统在死机时会产生一些dump信息&#xff0c;例如oops&#xff0c;通过分析oops信息就可以基本定位问题所在&#xff0c;但有些特殊情况下死机时&#xff0c;没有任何的打印的信息。如果直接使用printk等打印排查问题&#xff0c;有可能会因为print…

ssm+vue的校园一卡通密钥管理系统(有报告)。Javaee项目,ssm vue前后端分离项目。

演示视频&#xff1a; ssmvue的校园一卡通密钥管理系统&#xff08;有报告&#xff09;。Javaee项目&#xff0c;ssm vue前后端分离项目。 项目介绍&#xff1a; 采用M&#xff08;model&#xff09;V&#xff08;view&#xff09;C&#xff08;controller&#xff09;三层体系…

240207-3步设置VSCode插件Inline-Bookmarks自定义颜色及名称

Step 1: 插件安装 Step 2: 配置文件 "inline-bookmarks.expert.custom.styles": {"default": {"gutterIconColor": "#157EFB","overviewRulerColor": "rgba(21, 126, 251, 0.7)","light": {"fontW…

使用HCPpipelines分割皮层

前段时间阅读了一篇文献,文章的做法我比较感兴趣,所以打算学习一下文献的做法。文章的最开始一部分是使用HCPpipelines对T1和T2像进行皮层分割,调用的是freesurfer6。https://github.com/Washington-University/HCPpipelines 一、工作环境准备 1.安装好FSL,版本在6.0.2以上…

H2和流行关系型数据库对比

1.H2和SQLite数据库对比 1.1.独特的特点和用途 H2 和 SQLite 是两个流行的轻量级数据库&#xff0c;它们各自有一些独特的特点和用途&#xff1a; H2 数据库: 主要用于 Java 应用&#xff0c;因为它是用 Java 编写的。支持内存模式和磁盘持久化。提供了一个基于浏览器的控制台…