Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
IPC
2024-RSOC
#【RSOC】DAY3-IPC
发布于 2024-07-25 16:49:14 浏览:342
订阅该版
#【RSOC】DAY3-IPC ##定义 ```c #define THREAD_STACK_SIZE 1024 #define THREAD_PRIORITY 25 #define THREAD_TIMESLICE 5 static rt_mutex_t mutex = RT_NULL; static rt_mailbox_t mb = RT_NULL; // 消息邮箱的大小 #define MB_SIZE 32 ``` ##线程入口 ```c void thread1_entry(void *parameter) { int count = 0; while (1) { // 加锁保护共享资源 rt_mutex_take(mutex, RT_WAITING_FOREVER); // 发送消息到消息邮箱 if (rt_mb_send(mb, (rt_ubase_t)count) == RT_EOK) { rt_kprintf("Thread 1: sent message - %d\n", count); } else { rt_kprintf("Thread 1: failed to send message\n"); } count++; // 解锁 rt_mutex_release(mutex); rt_thread_mdelay(1000); // 延迟1000毫秒(1秒) } } // 线程2的入口函数 void thread2_entry(void *parameter) { rt_ubase_t msg; while (1) { // 从消息邮箱接收消息 if (rt_mb_recv(mb, &msg, RT_WAITING_FOREVER) == RT_EOK) { // 加锁保护共享资源 rt_mutex_take(mutex, RT_WAITING_FOREVER); rt_kprintf("Thread 2: received message - %d\n", msg); // 解锁 rt_mutex_release(mutex); } else { rt_kprintf("Thread 2: failed to receive message\n"); } } } ``` ##主函数 ```c int main(void) { rt_thread_t thread1 = RT_NULL; rt_thread_t thread2 = RT_NULL; // 创建互斥量 mutex = rt_mutex_create("mutex", RT_IPC_FLAG_PRIO); if (mutex == RT_NULL) { rt_kprintf("Failed to create mutex\n"); return -1; } // 创建消息邮箱 mb = rt_mb_create("mb", MB_SIZE, RT_IPC_FLAG_PRIO); if (mb == RT_NULL) { rt_kprintf("Failed to create mailbox\n"); rt_mutex_delete(mutex); return -1; } // 创建线程1 thread1 = rt_thread_create("thread1", thread1_entry, RT_NULL, THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE); // 如果线程1创建成功,启动线程1 if (thread1 != RT_NULL) { rt_thread_startup(thread1); } else { rt_kprintf("Thread 1 creation failed!\n"); rt_mutex_delete(mutex); rt_mb_delete(mb); return -1; } // 创建线程2 thread2 = rt_thread_create("thread2", thread2_entry, RT_NULL, THREAD_STACK_SIZE, THREAD_PRIORITY + 1, // 设置不同的优先级以观察调度行为 THREAD_TIMESLICE); // 如果线程2创建成功,启动线程2 if (thread2 != RT_NULL) { rt_thread_startup(thread2); } else { rt_kprintf("Thread 2 creation failed!\n"); rt_mutex_delete(mutex); rt_mb_delete(mb); return -1; } return 0; } ``` ##实验结果 
0
条评论
默认排序
按发布时间排序
登录
注册新账号
关于作者
fanxiao
这家伙很懒,什么也没写!
文章
4
回答
0
被采纳
0
关注TA
发私信
相关文章
1
请问线程休眠时清除 errno 的设计意图是什么?
2
ipc对象插入时既有优先级模式,又有ffio模式会不会出bug?
3
我自己添加一个释放多个信号量的函数,会不会对系统有影响
4
rtthread IPC机制理解
5
如何实现多个线程对1个线程消息的订阅
6
请问Nano怎么使用IPC组件,是不是必须标准版才可以使用
7
RTT 多线程间通信机制有哪几种及推荐?
8
对于动态申请的IPC(邮箱,信号量),如何确保在释放的时候临界安全问题
9
调用rt_sem_take前是否要先发送RT_IPC_CMD_RESE...
10
驱动程序中的线程挂起与IPC的线程挂起流程为什么不一样?
推荐文章
1
RT-Thread应用项目汇总
2
玩转RT-Thread系列教程
3
国产MCU移植系列教程汇总,欢迎查看!
4
机器人操作系统 (ROS2) 和 RT-Thread 通信
5
五分钟玩转RT-Thread新社区
6
【技术三千问】之《玩转ART-Pi》,看这篇就够了!干货汇总
7
关于STM32H7开发板上使用SDIO接口驱动SD卡挂载文件系统的问题总结
8
STM32的“GPU”——DMA2D实例详解
9
RT-Thread隐藏的宝藏之completion
10
【ART-PI】RT-Thread 开启RTC 与 Alarm组件
热门标签
RT-Thread Studio
串口
Env
LWIP
SPI
AT
Bootloader
Hardfault
CAN总线
FinSH
ART-Pi
DMA
USB
文件系统
RT-Thread
SCons
RT-Thread Nano
线程
MQTT
STM32
FAL
RTC
rt-smart
I2C_IIC
cubemx
UART
ESP8266
WIZnet_W5500
BSP
ota在线升级
PWM
flash
packages_软件包
freemodbus
潘多拉开发板_Pandora
ADC
GD32
定时器
编译报错
flashDB
keil_MDK
socket
中断
rt_mq_消息队列_msg_queue
Debug
ulog
SFUD
msh
C++_cpp
at_device
本月问答贡献
出出啊
1524
个答案
343
次被采纳
小小李sunny
1444
个答案
290
次被采纳
张世争
818
个答案
179
次被采纳
crystal266
555
个答案
162
次被采纳
whj467467222
1222
个答案
149
次被采纳
本月文章贡献
出出啊
1
篇文章
1
次点赞
小小李sunny
1
篇文章
1
次点赞
张世争
1
篇文章
2
次点赞
crystal266
2
篇文章
1
次点赞
whj467467222
2
篇文章
1
次点赞
回到
顶部
发布
问题
投诉
建议
回到
底部