Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
2024-RSOC
星火1号_spark_星火一号_开发板
关于rtthread内核的线程间的通信和同步
发布于 2024-07-24 23:26:25 浏览:509
订阅该版
以下为创建两个动态线程,一个静态线程,加上消息队列和信号量,以此实现rtthread的通信和同步的一个简单小示例 ```c /* * Copyright (c) 2006-2023, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes * 2022-06-29 Rbb666 first version */ #include "board.h" #include
#include
#include
#include
#include "drv_gpio.h" #define LED_PIN GET_PIN(F, 11) #define LED_PIN2 GET_PIN(F, 12) #define KEY GET_PIN(C, 1) //静态创建线程 static struct rt_thread thread1; static rt_uint8_t rt_thread1_stack[1024]; static void thread1_entry(void *parameter); //动态创建线程 static rt_thread_t thread2 = RT_NULL; static void thread2_entry(void *parameter); #define RTTHREAD_STACK 512 #define RTTHREAD_PRIORITY 4 #define RTTHREAD_TIMER 50 static rt_thread_t thread3=RT_NULL; static void thread3_entry(void *parameter); #define thread3_priority 3 #define thread3_timer 40 //线程三 static rt_thread_t thread_sem=RT_NULL; static void thread_sem_entry(void *para); //消息队列 static rt_mq_t test_mq=RT_NULL; struct msg{ rt_uint8_t *data_ptr; rt_uint32_t data_size; }; //定时器和信号量 static rt_timer_t timers = RT_NULL; static rt_sem_t sem = RT_NULL; static uint32_t timer_count = 0; static void timer_callback(void *para); uint32_t i=0; uint32_t t=0; int main(void) { rt_pin_mode(LED_PIN, PIN_MODE_OUTPUT); rt_pin_mode(LED_PIN2, PIN_MODE_OUTPUT); rt_pin_mode(KEY,PIN_MODE_INPUT); //创建消息队列 test_mq=rt_mq_create("thread_mq", 512, 20, RT_IPC_FLAG_FIFO); if(test_mq != RT_EOK){ rt_kprintf("消息队列,FAILED mq1 \n");} else { rt_kprintf("success create"); } //定时器 timers = rt_timer_create("timers", timer_callback, RT_NULL, 1000, RT_TIMER_FLAG_PERIODIC|RT_TIMER_FLAG_SOFT_TIMER); if(timers!=RT_NULL) rt_timer_start(timers); //信号量 sem = rt_sem_create("test_sem", 5, RT_IPC_FLAG_FIFO); //线程一 rt_thread_init(&thread1, "init_thread1", thread1_entry, RT_NULL, &rt_thread1_stack[0], sizeof(rt_thread1_stack), 6, 20); rt_thread_startup(&thread1); //线程二 thread2=rt_thread_create("thread2", thread2_entry,RT_NULL, RTTHREAD_STACK, RTTHREAD_PRIORITY, RTTHREAD_TIMER); if(thread2!=RT_NULL){ rt_thread_startup(thread2); }else { return 1; } //线程三 thread3 = rt_thread_create("thread3", thread3_entry, RT_NULL, RTTHREAD_STACK, thread3_priority, thread3_timer); if(thread3!=RT_NULL){ rt_thread_startup(thread3); }else { return 1; } //有关信号量的线程 thread_sem = rt_thread_create("thread_sem", thread_sem_entry, RT_NULL, 512, 10, 20); if(thread_sem!=RT_NULL){ rt_thread_startup(thread_sem); }else { return 1; } for (;;) { rt_thread_mdelay(1000); rt_pin_write(LED_PIN, PIN_HIGH); rt_pin_write(LED_PIN2, PIN_LOW); rt_thread_mdelay(500); rt_pin_write(LED_PIN, PIN_LOW); rt_pin_write(LED_PIN2, PIN_HIGH); } } static void thread1_entry(void *parameter){ uint16_t i; rt_err_t sign=RT_EOK; for(i=0;i<15;i++){ rt_kprintf("thread1 count is %d\n",i); rt_thread_mdelay(20); } rt_kprintf("thread1 is already end \n"); } static void thread2_entry(void *parameter){ rt_err_t uwret=RT_EOK; uint16_t i; char *r_recv; struct msg msg_ptr; msg_ptr.data_ptr; for(i=20;i>0;i--){ rt_kprintf("thread2 count2 are %d\n",i); rt_thread_mdelay(50); } rt_kprintf("thread2 is already out\n"); while(1){ uwret = rt_mq_recv(test_mq, &r_recv,sizeof(r_recv), RT_WAITING_FOREVER); if(uwret==RT_EOK){ rt_kprintf("success receive %s \n",r_recv); }else { rt_kprintf("no receive \n"); } rt_thread_delay(200); } } static void thread3_entry(void *parameter){ uint16_t i; rt_err_t uwret; char buf='Z'; uint16_t buf2=1; struct msg msg_ptr; msg_ptr.data_ptr="this is a queue"; msg_ptr.data_size=20; char *s1 = "HELLO"; while(1){ if(rt_pin_read(KEY)==PIN_LOW){ uwret=rt_mq_send(test_mq, &s1, sizeof(s1)); } if(uwret==RT_EOK){ rt_kprintf("success mq send %s \n",s1); } rt_thread_mdelay(20); } } static void timer_callback(void *para){ t++; rt_err_t uwret=RT_EOK; uwret = rt_sem_release(sem); if(RT_EOK==uwret){ rt_kprintf("timer is DI %d CI\n",t); rt_kprintf("success send \n"); }else { rt_kprintf("failed \n"); } } static void thread_sem_entry(void *para){ rt_err_t uwret=RT_EOK; while(1){ uwret = rt_sem_take(sem, RT_WAITING_FOREVER); if(RT_EOK==uwret){ i++; rt_kprintf("111,success received"); rt_kprintf(",sem ok success IS %d CI \n",i); } } } ``` ![微信图片_20240724232128.png](https://oss-club.rt-thread.org/uploads/20240724/5881880a4577eec78e539105bf61c9dc.png) ![微信图片_20240724232113.png](https://oss-club.rt-thread.org/uploads/20240724/c7127714e34f168af3605a0788487411.png) ![微信图片_20240724232525.png](https://oss-club.rt-thread.org/uploads/20240724/79b580e53f0f68d63699051aff7bcf9b.png)
0
条评论
默认排序
按发布时间排序
登录
注册新账号
关于作者
CHNT_8350
这家伙很懒,什么也没写!
文章
9
回答
0
被采纳
0
关注TA
发私信
相关文章
1
[星火一号] 代码模板, 手动写启动代码, 开机后 snprintf 不能处理 %llu 了, 是有什么配置上的冲突吗?
2
使用MDK5.37开发星火一号,双击mklinks.bat 文件后,目录下没有 rt-thread 和 libraries 的文件夹图标。
3
studio文件构建丢失
4
rtt中星火一号stm-32怎么把两个示例工程合并成一个
5
星火一号串口发送问题
6
基于开发板建工程的疑问
7
使用星火一号开发板建工程的奇怪问题
8
星火一号板pwm功能,不报错,但也不输出,为什么?
9
星火一号怎么强制改变已占用的引脚的功能呢
10
火星一号的标准库在哪里?外设的数据手册在哪里看?
推荐文章
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
USB
DMA
文件系统
RT-Thread
SCons
RT-Thread Nano
线程
MQTT
STM32
RTC
FAL
rt-smart
ESP8266
I2C_IIC
WIZnet_W5500
ota在线升级
UART
PWM
cubemx
freemodbus
flash
packages_软件包
BSP
潘多拉开发板_Pandora
定时器
ADC
GD32
flashDB
socket
中断
Debug
编译报错
msh
SFUD
keil_MDK
rt_mq_消息队列_msg_queue
at_device
ulog
C++_cpp
本月问答贡献
踩姑娘的小蘑菇
7
个答案
3
次被采纳
张世争
8
个答案
2
次被采纳
rv666
5
个答案
2
次被采纳
用户名由3_15位
11
个答案
1
次被采纳
KunYi
6
个答案
1
次被采纳
本月文章贡献
程序员阿伟
6
篇文章
2
次点赞
hhart
3
篇文章
4
次点赞
大龄码农
1
篇文章
2
次点赞
ThinkCode
1
篇文章
1
次点赞
Betrayer
1
篇文章
1
次点赞
回到
顶部
发布
问题
投诉
建议
回到
底部