Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
线程
关于mutex_sample例程
发布于 2021-07-07 20:58:21 浏览:703
订阅该版
```c /* * 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 */ /* * 程序清单:互斥锁例程 * * 互斥锁是一种保护共享资源的方法。当一个线程拥有互斥锁的时候, * 可以保护共享资源不被其他线程破坏。线程1对2个number分别进行加1操作 * 线程2也会对2个number分别进行加1操作。使用互斥量保证2个number值保持一致 */ #include
#define THREAD_PRIORITY 8 #define THREAD_TIMESLICE 5 /* 指向互斥量的指针 */ static rt_mutex_t dynamic_mutex = RT_NULL; static rt_uint8_t number1,number2 = 0; ALIGN(RT_ALIGN_SIZE) static char thread1_stack[1024]; static struct rt_thread thread1; static void rt_thread_entry1(void *parameter) { while(1) { rt_kprintf("in thread 1 \n"); /* 线程1获取到互斥量后,先后对number1、number2进行加1操作,然后释放互斥量 */ //rt_mutex_take(dynamic_mutex, RT_WAITING_FOREVER); number1++; rt_thread_mdelay(10); number2++; //rt_mutex_release(dynamic_mutex); } } ALIGN(RT_ALIGN_SIZE) static char thread2_stack[1024]; static struct rt_thread thread2; static void rt_thread_entry2(void *parameter) { while(1) { /* 线程2获取到互斥量后,检查number1、number2的值是否相同,相同则表示mutex起到了锁的作用 */ //rt_mutex_take(dynamic_mutex, RT_WAITING_FOREVER); if(number1 != number2) { rt_kprintf("not protect.number1 = %d, mumber2 = %d \n",number1 ,number2); } else { rt_kprintf("mutex protect ,number1 = mumber2 is %d\n",number1); } number1++; number2++; //rt_mutex_release(dynamic_mutex); if(number1 >=50) return; } } /* 互斥量示例的初始化 */ int mutex_sample(void) { /* 创建一个动态互斥量 */ dynamic_mutex = rt_mutex_create("dmutex", RT_IPC_FLAG_FIFO); if (dynamic_mutex == RT_NULL) { rt_kprintf("create dynamic mutex failed.\n"); return -1; } rt_thread_init(&thread1, "thread1", rt_thread_entry1, RT_NULL, &thread1_stack[0], sizeof(thread1_stack), THREAD_PRIORITY, THREAD_TIMESLICE); rt_thread_startup(&thread1); rt_thread_init(&thread2, "thread2", rt_thread_entry2, RT_NULL, &thread2_stack[0], sizeof(thread2_stack), THREAD_PRIORITY-1, THREAD_TIMESLICE); rt_thread_startup(&thread2); return 0; } /* 导出到 msh 命令列表中 */ MSH_CMD_EXPORT(mutex_sample, mutex sample); ``` ``` \ | / - RT - Thread Operating System / | \ 3.1.0 build Jul 7 2021 2006 - 2018 Copyright by rt-thread team msh >mu mutex_sample msh >mutex_sample in thread 1 not protect.number1 = 1, mumber2 = 0 not protect.number1 = 2, mumber2 = 1 not protect.number1 = 3, mumber2 = 2 not protect.number1 = 4, mumber2 = 3 not protect.number1 = 5, mumber2 = 4 not protect.number1 = 6, mumber2 = 5 not protect.number1 = 7, mumber2 = 6 not protect.number1 = 8, mumber2 = 7 not protect.number1 = 9, mumber2 = 8 not protect.number1 = 10, mumber2 = 9 not protect.number1 = 11, mumber2 = 10 not protect.number1 = 12, mumber2 = 11 not protect.number1 = 13, mumber2 = 12 not protect.number1 = 14, mumber2 = 13 not protect.number1 = 15, mumber2 = 14 not protect.number1 = 16, mumber2 = 15 not protect.number1 = 17, mumber2 = 16 not protect.number1 = 18, mumber2 = 17 not protect.number1 = 19, mumber2 = 18 not protect.number1 = 20, mumber2 = 19 not protect.number1 = 21, mumber2 = 20 not protect.number1 = 22, mumber2 = 21 not protect.number1 = 23, mumber2 = 22 not protect.number1 = 24, mumber2 = 23 not protect.number1 = 25, mumber2 = 24 not protect.number1 = 26, mumber2 = 25 not protect.number1 = 27, mumber2 = 26 not protect.number1 = 28, mumber2 = 27 not protect.number1 = 29, mumber2 = 28 not protect.number1 = 30, mumber2 = 29 not protect.number1 = 31, mumber2 = 30 not protect.number1 = 32, mumber2 = 31 not protect.number1 = 33, mumber2 = 32 not protect.number1 = 34, mumber2 = 33 not protect.number1 = 35, mumber2 = 34 not protect.number1 = 36, mumber2 = 35 not protect.number1 = 37, mumber2 = 36 not protect.number1 = 38, mumber2 = 37 not protect.number1 = 39, mumber2 = 38 not protect.number1 = 40, mumber2 = 39 not protect.number1 = 41, mumber2 = 40 not protect.number1 = 42, mumber2 = 41 not protect.number1 = 43, mumber2 = 42 not protect.number1 = 44, mumber2 = 43 not protect.number1 = 45, mumber2 = 44 not protect.number1 = 46, mumber2 = 45 not protect.number1 = 47, mumber2 = 46 not protect.number1 = 48, mumber2 = 47 not protect.number1 = 49, mumber2 = 48 in thread 1 msh >in thread 1 in thread 1 in thread 1 ... ``` 如上代码,在mutex_sample历程中,两个线程的优先级, thread1为THREAD_PRIORITY, thread2为THREAD_PRIORITY-1, 我把互斥量操作去掉后,也就相当于两个普通线程,为什么低优先级的thread1在最开始可以先执行一些? 按我的理解,因为它优先级低,应该在开始一点都不能执行才对, 只有thread2执行完了它才能开始执行.
查看更多
游走在01的海洋
2021-07-07
In the end, it's not the years in your life that count. It's the life in your years.
一、首先mutex_sample函数执行后,先初始化并启动了thread1;这时系统会判定启动的thread1是否为当前优先级最高线程(此时thread2还未初始化),如果时,先执行thread1;thread1中遇到延时函数会挂起当前线程,转会到mutex_sample函数; 二、接下来就会初始化thread2,之后rt_thread_startup启动thread2;之后的过程和thread1一样;
2
个回答
默认排序
按发布时间排序
123
认证专家
2021-07-07
这家伙很懒,什么也没写!
因为代码是`rt_thread_startup(&thread1);` 之后又执行`rt_thread_startup(&thread2);` 所以你就是先让线程1执行,等到线程2 `rt_thread_startup`的时候,才让线程2抢占线程1去执行。
撰写答案
登录
注册新账号
关注者
0
被浏览
703
关于作者
刘启锋
这家伙很懒,什么也没写!
提问
9
回答
2
被采纳
0
关注TA
发私信
相关问题
1
请问执行rt_thread_delete的操作后,线程还在运行是什么情况?
2
rtthread中,线程中的ADC采样率需求比时钟嘀嗒需求高怎么办?
3
有没有检测系统中有没有某个名字的线程的接口函数?
4
使用finsh 进行ota成功,线程里开ota失败
5
调度锁会引起线程内存不足
6
线程处于close状态消耗资源吗
7
paho_mqtt线程相关疑问
8
thread中不能使用rt_timer_start() 来开启定时器
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组件
最新文章
1
RTduino物联网应用零基础入门
2
TinyUSB Demo运行教程
3
RT-Thread学习大礼包一键带走!
4
freemodbus从机调试说明
5
【1024】瑞萨 RA 系列 BSP 制作与适配最新版本的 Keil 、 RSC、固件,较新的 FSP
热门标签
RT-Thread Studio
串口
Env
LWIP
SPI
AT
Bootloader
Hardfault
CAN总线
ART-Pi
FinSH
USB
DMA
文件系统
RT-Thread
SCons
RT-Thread Nano
线程
MQTT
STM32
RTC
FAL
rt-smart
ESP8266
I2C_IIC
WIZnet_W5500
ota在线升级
UART
cubemx
PWM
flash
packages_软件包
freemodbus
BSP
潘多拉开发板_Pandora
定时器
ADC
GD32
flashDB
socket
中断
Debug
编译报错
msh
SFUD
rt_mq_消息队列_msg_queue
keil_MDK
ulog
C++_cpp
at_device
本月问答贡献
张世争
8
个答案
2
次被采纳
踩姑娘的小蘑菇
3
个答案
2
次被采纳
用户名由3_15位
11
个答案
1
次被采纳
KunYi
5
个答案
1
次被采纳
bernard
1
个答案
1
次被采纳
本月文章贡献
出出啊
1
篇文章
2
次点赞
小小李sunny
1
篇文章
1
次点赞
张世争
1
篇文章
4
次点赞
crystal266
2
篇文章
2
次点赞
whj467467222
2
篇文章
1
次点赞
回到
顶部
发布
问题
分享
好友
手机
浏览
扫码手机浏览
投诉
建议
回到
底部