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

本文主要是介绍RTthread线程间通信(邮箱,消息队列,信号/软件中断)---01实际使用API函数,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!


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: 指向存储等到信号信息的指针

这篇关于RTthread线程间通信(邮箱,消息队列,信号/软件中断)---01实际使用API函数的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/682603

相关文章

Java中String字符串使用避坑指南

《Java中String字符串使用避坑指南》Java中的String字符串是我们日常编程中用得最多的类之一,看似简单的String使用,却隐藏着不少“坑”,如果不注意,可能会导致性能问题、意外的错误容... 目录8个避坑点如下:1. 字符串的不可变性:每次修改都创建新对象2. 使用 == 比较字符串,陷阱满

Python使用国内镜像加速pip安装的方法讲解

《Python使用国内镜像加速pip安装的方法讲解》在Python开发中,pip是一个非常重要的工具,用于安装和管理Python的第三方库,然而,在国内使用pip安装依赖时,往往会因为网络问题而导致速... 目录一、pip 工具简介1. 什么是 pip?2. 什么是 -i 参数?二、国内镜像源的选择三、如何

使用C++实现链表元素的反转

《使用C++实现链表元素的反转》反转链表是链表操作中一个经典的问题,也是面试中常见的考题,本文将从思路到实现一步步地讲解如何实现链表的反转,帮助初学者理解这一操作,我们将使用C++代码演示具体实现,同... 目录问题定义思路分析代码实现带头节点的链表代码讲解其他实现方式时间和空间复杂度分析总结问题定义给定

Linux使用nload监控网络流量的方法

《Linux使用nload监控网络流量的方法》Linux中的nload命令是一个用于实时监控网络流量的工具,它提供了传入和传出流量的可视化表示,帮助用户一目了然地了解网络活动,本文给大家介绍了Linu... 目录简介安装示例用法基础用法指定网络接口限制显示特定流量类型指定刷新率设置流量速率的显示单位监控多个

JavaScript中的reduce方法执行过程、使用场景及进阶用法

《JavaScript中的reduce方法执行过程、使用场景及进阶用法》:本文主要介绍JavaScript中的reduce方法执行过程、使用场景及进阶用法的相关资料,reduce是JavaScri... 目录1. 什么是reduce2. reduce语法2.1 语法2.2 参数说明3. reduce执行过程

如何使用Java实现请求deepseek

《如何使用Java实现请求deepseek》这篇文章主要为大家详细介绍了如何使用Java实现请求deepseek功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1.deepseek的api创建2.Java实现请求deepseek2.1 pom文件2.2 json转化文件2.2

Java调用DeepSeek API的最佳实践及详细代码示例

《Java调用DeepSeekAPI的最佳实践及详细代码示例》:本文主要介绍如何使用Java调用DeepSeekAPI,包括获取API密钥、添加HTTP客户端依赖、创建HTTP请求、处理响应、... 目录1. 获取API密钥2. 添加HTTP客户端依赖3. 创建HTTP请求4. 处理响应5. 错误处理6.

python使用fastapi实现多语言国际化的操作指南

《python使用fastapi实现多语言国际化的操作指南》本文介绍了使用Python和FastAPI实现多语言国际化的操作指南,包括多语言架构技术栈、翻译管理、前端本地化、语言切换机制以及常见陷阱和... 目录多语言国际化实现指南项目多语言架构技术栈目录结构翻译工作流1. 翻译数据存储2. 翻译生成脚本

C++ Primer 多维数组的使用

《C++Primer多维数组的使用》本文主要介绍了多维数组在C++语言中的定义、初始化、下标引用以及使用范围for语句处理多维数组的方法,具有一定的参考价值,感兴趣的可以了解一下... 目录多维数组多维数组的初始化多维数组的下标引用使用范围for语句处理多维数组指针和多维数组多维数组严格来说,C++语言没

在 Spring Boot 中使用 @Autowired和 @Bean注解的示例详解

《在SpringBoot中使用@Autowired和@Bean注解的示例详解》本文通过一个示例演示了如何在SpringBoot中使用@Autowired和@Bean注解进行依赖注入和Bean... 目录在 Spring Boot 中使用 @Autowired 和 @Bean 注解示例背景1. 定义 Stud