当前位置:网站首页>RT_ Use of thread message queue
RT_ Use of thread message queue
2022-07-28 04:53:00 【The endeavor】
One 、 The working mechanism of message queue
The message queue is RT_thread Another commonly used way of communication between threads , Message queuing is an extension of the mailbox .
Message queues can receive messages of varying length from threads or interrupt service routines , And cache messages in your own memory space , Other threads can read messages from the message queue and process them accordingly .
Two 、 Control block of message queue

Through the operation of message queue 
The basic content is similar to that of email , I won't explain it in detail !!!
/* * 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 */
/* * Program listing : Message queuing routines * * This program will create 2 A dynamic thread , A thread will receive messages from the message queue ; A thread will cancel regularly * Message queue sending General and emergency messages . */
#include <rtthread.h>
/* Message queuing control block */
static struct rt_messagequeue mq;
/* The memory pool used in the message queue to place messages */
static rt_uint8_t msg_pool[2048];
ALIGN(RT_ALIGN_SIZE)
static char thread1_stack[1024];
static struct rt_thread thread1;
/* Threads 1 Entry function */
static void thread1_entry(void *parameter)
{
char buf = 0;
rt_uint8_t cnt = 0;
while (1)
{
/* Receive messages from the message queue */
if (rt_mq_recv(&mq, &buf, sizeof(buf), RT_WAITING_FOREVER) == RT_EOK)
{
rt_kprintf("thread1: recv msg from msg queue, the content:%c\n", buf);
if (cnt == 19)
{
break;
}
}
/* Time delay 50ms */
cnt++;
rt_thread_mdelay(50);
}
rt_kprintf("thread1: detach mq \n");
rt_mq_detach(&mq);
}
ALIGN(RT_ALIGN_SIZE)
static char thread2_stack[1024];
static struct rt_thread thread2;
/* Threads 2 entrance */
static void thread2_entry(void *parameter)
{
int result;
char buf = 'A';
rt_uint8_t cnt = 0;
while (1)
{
if (cnt == 8)
{
/* Send an emergency message to the message queue */
result = rt_mq_urgent(&mq, &buf, 1);
if (result != RT_EOK)
{
rt_kprintf("rt_mq_urgent ERR\n");
}
else
{
rt_kprintf("thread2: send urgent message - %c\n", buf);
}
}
else if (cnt >= 20)/* send out 20 Exit after message */
{
rt_kprintf("message queue stop send, thread2 quit\n");
break;
}
else
{
/* Send a message to the message queue */
result = rt_mq_send(&mq, &buf, 1);
if (result != RT_EOK)
{
rt_kprintf("rt_mq_send ERR\n");
}
rt_kprintf("thread2: send message - %c\n", buf);
}
buf++;
cnt++;
/* Time delay 5ms */
rt_thread_mdelay(5);
}
}
/* Initialization of the message queue example */
int msgq_sample(void)
{
rt_err_t result;
/* Initialize message queue */
result = rt_mq_init(&mq,
"mqt",
&msg_pool[0], /* The memory pool points to msg_pool */
1, /* The size of each message is 1 byte */
sizeof(msg_pool), /* The size of the memory pool is msg_pool Size */
RT_IPC_FLAG_FIFO); /* If there are multiple threads waiting , Allocate messages on a first come, first served basis */
if (result != RT_EOK)
{
rt_kprintf("init message queue failed.\n");
return -1;
}
rt_thread_init(&thread1,
"thread1",
thread1_entry,
RT_NULL,
&thread1_stack[0],
sizeof(thread1_stack), 25, 5);
rt_thread_startup(&thread1);
rt_thread_init(&thread2,
"thread2",
thread2_entry,
RT_NULL,
&thread2_stack[0],
sizeof(thread2_stack), 25, 5);
rt_thread_startup(&thread2);
return 0;
}
/* Export to msh In the command list */
MSH_CMD_EXPORT(msgq_sample, msgq sample);
边栏推荐
- When initializing with pyqt5, super() and_ init _ () problems faced by the coordinated use of functions, as well as the corresponding learning and solutions
- Redux basic syntax
- 【sylar】框架篇-Chapter6-协程调度模块
- Rendering process, how the code becomes a page (2)
- What should testers know about login security?
- [函数文档] torch.histc 与 paddle.histogram 与 numpy.histogram
- Artificial intelligence and RPA technology application (I) -rpa Hongji product introduction, designer interface function explanation
- What is the core value of testing?
- [每日一氵]上古年代的 Visual Studio2015 安装
- Redis configuration file explanation / parameter explanation and elimination strategy
猜你喜欢

Tiantian AMADA CNC bending machine touch screen maintenance rgm21003 host circuit board maintenance
![[daily question 1] 735. Planetary collision](/img/ba/0ef08ff874f1ddad3fa79b01dba61f.png)
[daily question 1] 735. Planetary collision
![[Oracle] 083 wrong question set](/img/10/9a5dae9542a8fed0356843c59f3c2f.png)
[Oracle] 083 wrong question set

Easycvr Video Square snapshot adding device channel offline reason display

Domain name (subdomain name) collection method of Web penetration

机器人教育在STEM课程中的设计研究

全方位分析STEAM和创客教育的差异化

Ma Yi, Shen Xiangyang, Cao Ying's latest AI overview is hot! It took 3 months to build, netizens: required papers

Phpstorm2022 connect to the database

Leetcode 454. Adding four numbers II
随机推荐
提升学生群体中的STEAM教育核心素养
Phpstorm2022 connect to the database
MySQL数据库————初识数据库
Cmake usage base summary
Web渗透之域名(子域名)收集方法
pytorch打包exe出现WARNING: file already exists but should not: C:\Users\workAI\AppData\Local\Temp\_MEI13
[daily one] visual studio2015 installation in ancient times
Cloudcompare & PCL point cloud least square fitting plane
[Sylar] framework Chapter 6 collaborative scheduling module
[Hongke technology] Application of network Multimeter in data center
【sylar】实战篇-基于 redis 的参数查询服务
猿辅导技术进化论:助力教与学 构想未来学校
塑料可以执行GB/T 2408 -燃烧性能的测定吗
Chuangyuan will join hands with 50+ cloud native enterprises to explore new models to cross the digital divide
Use and expansion of fault tolerance and fusing
Strlen introduction, and the difference between sizeof
Ma Yi, Shen Xiangyang, Cao Ying's latest AI overview is hot! It took 3 months to build, netizens: required papers
[Sylar] framework -chapter9-hook module
What is the reason why the easycvr national standard protocol access equipment is online but the channel is not online?
Analyze the emotional elements contained in intelligent sweeping robot