当前位置:网站首页>嵌入式开发基础之线程间通信
嵌入式开发基础之线程间通信
2022-06-24 15:45:00 【跋扈洋】
引言
在裸机编程中,经常会使用全局变量进行功能间的通信,如某些功能可能由于一些操作而改变全局变量的值,另一个功能对此全局变量进行读取,根据读取到的全局变量值执行相应的动作,达到通信协作的目的。而实时操作系统往往采用邮箱、消息队列、信号用于线程间的通信。
基本概念
邮箱
邮箱服务是实时操作系统中一种典型的线程间通信方法。 邮箱用于线程间通信,特点是开销比较低,效率较高。典型的邮箱也称作交换消息。 非阻塞方式的邮件发送过程能够安全的应用于中断服务中,是线程、中断服务、定时器向线程发送消息的有效手段。通常来说,邮件收取过程可能是阻塞的,这取决于邮箱中是否有邮件,以及收取邮件时设置的超时时间。当邮箱中不存在邮件且超时时间不为 0 时,邮件收取过程将变成阻塞方式。在这类情况下,只能由线程进行邮件的收取。 当一个线程向邮箱发送邮件时,如果邮箱没满,将把邮件复制到邮箱中。如果邮箱已经满了,发送线程可以设置超时时间,选择等待挂起或直接返回特定值。如果发送线程选择挂起等待,那么当邮箱中的邮件被收取而空出空间来时,等待挂起的发送线程将被唤醒继续发送。 当一个线程从邮箱中接收邮件时,如果邮箱是空的,接收线程可以选择是否等待挂起直到收到新的邮件而唤醒,或可以设置超时时间。当达到设置的超时时间,邮箱依然未收到邮件时,这个选择超时等待的线程将被唤醒并返回特定值。如果邮箱中存在邮件,那么接收线程将复制邮箱中的 特定大小邮件到接收缓存中。
消息队列
消息队列的基本概念
消息队列是一种常用的线程间通讯方式,是邮箱的扩展。可以应用在多种场合:线程间的消息交换、使用串口接收不定长数据等。
消息队列的工作机制
消息队列能够接收来自线程或中断服务例程中不固定长度的消息,并把消息缓存在自己的内存空间中。其他线程也能够从消息队列中读取相应的消息,而当消息队列是空的时候,可以挂起读取线程。当有新的消息到达时,挂起的线程将被唤醒以接收并处理消息。消息队列是一种异步的通信方式。消息队列是一种常用于任务间通信的数据结构,队列可以在任务与任务间、中断和任务间传递信息,实现了任务接收来自其他任务或中断的不固定长度的消息,任务能够从队列里面读取消息,当队列中的消息是空时,读取消息的任务将被阻塞,用户还可以指定阻塞的任务时间,在这段时中,如果队列为空,该任务将保持阻塞状态以等待队列数据有效。当队列中有新消息时,被阻塞的任务会被唤醒并处理新消息;当等待的时间超过了指定的阻塞时间,即使队列中尚无有效数据,任务也会自动从阻塞态转为就绪态。
当消息队列被创建时,它就被分配了消息队列控制块:消息队列名称、内存缓冲区、消息大小以及队列长度等。同时每个消息队列对象中包含着多个消息框,每个消息框可以存放一条消息;消息队列中的第一个和最后一个消息框被分别称为消息链表头和消息链表尾。创建消息队列时 FreeRTOS 会先给消息队列分配一块内存空间,这块内存的大小等于消息队列控制块大小加上(单个消息空间大小与消息队列长度的乘积),接着再初始化消息队列,此时消息队列为空。 同时每个消息队列都与消息空间在同一段连续的内存空间中,在创建成功的时候,这些内存就被占用了,只有删除了消息队列的时候,这段内存才会被释放掉,创建成功的时候就已经分配好每个消息空间与消息队列的容量,无法更改,每个消息空间可以存放不大于消息大小 uxItemSize 的任意类型的数据,所有消息队列中的消息空间总数即是消息队列的长度,这个长度可在消息队列创建时指定。 任务或者中断服务程序都可以给消息队列发送消息,当发送消息时,如果队列未满或者允许覆盖入队,RTOS 会将消息拷贝到消息队列队尾,否则,会根据用户指定的阻塞超时时间进行阻塞,在这段时间中,如果队列一直不允许入队,该任务将保持阻塞状态以等待队列允许入队。当其它任务从其等待的队列中读取入了数据(队列未满),该任务将自动由阻塞态转移为就绪态。当等待的时间超过了指定的阻塞时间,即使队列中还不允许入队,任务也会自动从阻塞态转移为就绪态,此时发送消息的任务或者中断程序会收到一个错误码。 发送紧急消息的过程与发送消息几乎一样,唯一的不同是,当发送紧急消息时,发送的位置是消息队列队头而非队尾,这样,接收者就能够优先接收到紧急消息,从而及时进行消息处理。 当某个任务试图读一个队列时,其可以指定一个阻塞超时时间。在这段时间中,如果队列为空,该任务将保持阻塞状态以等待队列数据有效。当其它任务或中断服务程序往其等待的队列中写入了数据,该任务将自动由阻塞态转移为就绪态。当等待的时间超过了指定的阻塞时间,即使队列中尚无有效数据,任务也会自动从阻塞态转移为就绪态。
边栏推荐
- Global and Chinese market for commercial barbecue smokers 2022-2028: Research Report on technology, participants, trends, market size and share
- 一文详解JackSon配置信息
- D. Solve The Maze(思维+bfs)Codeforces Round #648 (Div. 2)
- 2021-04-27: if the adjacent position of a character does not have the same character
- How to easily realize online karaoke room and sing "mountain sea" with Wang Xinling
- Linux record -4.22 MySQL 5.37 installation (supplementary)
- MySQL development specification
- nifi从入门到实战(保姆级教程)——环境篇
- MySQL binlog
- One article explains Jackson configuration information in detail
猜你喜欢
Database tools in intelij can connect but cannot display schema, tables
Siggraph 2022 | truly restore the hand muscles. This time, the digital human hands have bones, muscles and skin
存在安全隐患 部分冒险家混动版将召回
实现领域驱动设计 - 使用ABP框架 - 领域逻辑 & 应用逻辑
The equipment is connected to the easycvr platform through the national standard gb28181. How to solve the problem of disconnection?
How to easily realize online karaoke room and sing "mountain sea" with Wang Xinling
Apple is no match for the longest selling mobile phone made in China, and has finally brought back the face of the domestic mobile phone
I just came back from the Ali software test. I worked for Alibaba P7 in 3+1, with an annual salary of 28*15
Recommend several super practical data analysis tools
Mongodb Getting started Practical Tutoriel: Learning Summary Table des matières
随机推荐
Binary computing
Instruction document for online written examination assistance of smart side school recruitment
Here comes Wi Fi 7. How strong is it?
2021-05-01: given an ordered array arr, it represents the points located on the X axis. Given a positive number k
How to easily realize online karaoke room and sing "mountain sea" with Wang Xinling
Detailed explanation of estab of Stata regression table output
The decline of China's product managers: starting from the nostalgia for jobs
我与“Apifox”的网络情缘
构建Go命令行程序工具链
Product level design of a project in SAP mm
2021-05-02: given the path of a file directory, write a function
Precautions for using JMeter suite to build a pressure test environment
Implement Domain Driven Design - use ABP framework - domain logic & application logic
Mongodb Getting started Practical Tutoriel: Learning Summary Table des matières
Global and Chinese market of inverted syrup 2022-2028: Research Report on technology, participants, trends, market size and share
The catch-up of domestic chips has scared Qualcomm, the leader of mobile phone chips in the United States, and made moves to cope with the competition
April 30, 2021: there are residential areas on a straight line, and the post office can only be built on residential areas. Given an ordered positive array arr
How to implement SQLSERVER database migration in container
Wi-Fi 7 来啦,它到底有多强?
Golang+redis reentrant lock