当前位置:网站首页>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);
边栏推荐
- Gerrit operation - rollback a patch_ set
- (2.4) [service Trojan -slimftp] introduction and use
- If mongoose exists, it will be updated; if it does not exist, it will be added
- Nat fundamentals and private IP
- How to upgrade a pair of 12.2 RAC(primary) and a pair of 12.2 RAC(dataguard) to 19c
- Dynamic SQL and paging
- The first artificial intelligence security competition starts. Three competition questions are waiting for you to fight
- Can plastics comply with gb/t 2408 - Determination of flammability
- How to quickly locate bugs? How to write test cases?
- Cmake usage base summary
猜你喜欢

低代码是开发的未来吗?浅谈低代码平台

Strlen introduction, and the difference between sizeof

Dynamic SQL and paging

提升学生群体中的STEAM教育核心素养

Leetcode 18. sum of four numbers

What SaaS architecture design do you need to know?

Transformer landing | next vit realizes the real-time landing of industrial tensorrt, surpassing RESNET and cswin

Leetcode 454. Adding four numbers II

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

Zhejiang University and other recent review papers on deep learning new drug design
随机推荐
(克隆虚拟机步骤)
How to upgrade a pair of 12.2 RAC(primary) and a pair of 12.2 RAC(dataguard) to 19c
Basic knowledge of network security - password (I)
Testcafe provides automatic waiting mechanism and live operation mode
The difference between alter and confirm, prompt
Sort - cardinal sort
Wang Shuang assembly language detailed learning notes 3: registers (memory access)
Redis configuration file explanation / parameter explanation and elimination strategy
Easycvr Video Square snapshot adding device channel offline reason display
【sylar】框架篇-Chapter12-ByteArray 模块
MySQL partition table transformation
[Sylar] framework chapter -chapter21- environment variable module
Mac installs mysql5.7 through brew
With a monthly salary of 15.5K, he failed to start a business and was heavily in debt. How did he reverse the trend through software testing?
[Sylar] framework -chapter12 bytearray module
Rendering process, how the code becomes a page (I)
Take out system file upload
Rendering process, how the code becomes a page (2)
阿里巴巴面试题【杭州多测师】【杭州多测师_王sir】
Chuangyuan will join hands with 50+ cloud native enterprises to explore new models to cross the digital divide