当前位置: 首页 > news >正文

【STM32 FreeRTOS】信号量与互斥锁

二值信号量

二值信号量的本质是一个队列长度为1的队列,该队列就只有空和满两种情况,这就是二值。

二值信号量通常用于互斥访问或任务同步,与互斥信号量比较类似,但是二值信号量有可能会导致优先级翻转的问题,所以二值信号量更适合用于同步。

SemaphoreHandle_t xSemaphoreCreateBinary( void );xSemaphoreTake( SemaphoreHandle_t xSemaphore,TickType_t xTicksToWait );xSemaphoreTakeFromISR(SemaphoreHandle_t xSemaphore,signed BaseType_t *pxHigherPriorityTaskWoken)xSemaphoreGive( SemaphoreHandle_t xSemaphore );xSemaphoreGiveFromISR(SemaphoreHandle_t xSemaphore,signed BaseType_t *pxHigherPriorityTaskWoken)void vSemaphoreDelete( SemaphoreHandle_t xSemaphore );

重点在许多使用场景中,使用直达任务通知要比使用二值信号量的速度更快,内存效率更高。所以,没有实例代码。

计数型信号量

计数型信号量相当于队列长度大于1的队列,因此计数型信号量能够容纳多个资源,这在计数型信号量被创建的时候确定的。

SemaphoreHandle_t xSemaphoreCreateCounting( UBaseType_t uxMaxCount,UBaseType_t uxInitialCount);UBaseType_t uxSemaphoreGetCount( SemaphoreHandle_t xSemaphore );其他接口与二值信号量的接口一致。

重点在许多情况下,任务通知可以提供计数信号量的轻量级替代方案

互斥信号量(互斥锁)

互斥信号量其实就是一个拥有优先级继承的二值信号量,在同步的应用中二值信号量最合适。互斥信号量适合那些需要互斥访问的应用中。

注意:互斥信号量不能用于中断服务函数中,原因如下:

  • 互斥信号量有任务优先级继承的机制,但是中断不是任务,没有任务优先级,所以互斥信号量只能用于任务中。
  • 中断服务函数中不能因为要等待互斥信号量而设置阻塞时间进入阻塞态。
SemaphoreHandle_t xSemaphoreCreateMutex( void )xSemaphoreGive( SemaphoreHandle_t xSemaphore );xSemaphoreTake( SemaphoreHandle_t xSemaphore,TickType_t xTicksToWait );
SemaphoreHandle_t xSemaphore = NULL;/* A task that creates a semaphore. */
void vATask( void * pvParameters )
{/* Create the semaphore to guard a shared resource. As we are usingthe semaphore for mutual exclusion we create a mutex semaphorerather than a binary semaphore. */xSemaphore = xSemaphoreCreateMutex();
}/* A task that uses the semaphore. */
void vAnotherTask( void * pvParameters )
{/* ... Do other things. */if( xSemaphore != NULL ){/* See if we can obtain the semaphore. If the semaphore is notavailable wait 10 ticks to see if it becomes free. */if( xSemaphoreTake( xSemaphore, ( TickType_t ) 10 ) == pdTRUE ){/* We were able to obtain the semaphore and can now access theshared resource. *//* ... *//* We have finished accessing the shared resource. Release thesemaphore. */xSemaphoreGive( xSemaphore );}else{/* We could not obtain the semaphore and can therefore not accessthe shared resource safely. */}}
}

递归互斥信号量(递归互斥锁)

xSemaphoreCreateMutex()用于创建非递归互斥锁。非递归互斥锁只能被一个任务 获取一次,如果同一个任务想再次获取则会失败, 因为当任务第一次释放互斥锁时,互斥锁就一直 处于释放状态。

与非递归互斥锁相反,递归互斥锁可以被同一个任务获取很多次, 获取多少次就需要释放多少次, 此时才会返回递归互斥锁。

SemaphoreHandle_t xSemaphoreCreateRecursiveMutex( void )xSemaphoreTakeRecursive( SemaphoreHandle_t xMutex,TickType_t xTicksToWait );xSemaphoreGiveRecursive( SemaphoreHandle_t xMutex )
http://www.lryc.cn/news/424458.html

相关文章:

  • SP:eric 靶场复现【附代码】(权限提升)
  • SpringBoot项目启动直接结束--已解决
  • 【笔记】从零开始做一个精灵龙女-画贴图阶段(下)
  • React 学习——react项目中加入echarts图
  • 链表算法题一
  • Unity(2022.3.38LTS) - 基础概念
  • 无人机之飞手必看篇
  • 数据结构(11)——二叉搜索树
  • 如何使用和配置 AWS CLI 环境变量?
  • 七、流程控制
  • 【通过python启动指定的文件】
  • 区块链开源的项目有哪些?
  • 3152. 特殊数组 II(24.8.14)
  • Android 全系统版本文件读写最佳适配,CV 即用(适配到 Android 14)
  • 【日记】朋友和他女朋友领证了(368 字)
  • 行业大模型:信用评分大模型、生产优化大模型、库存管理大模型、物流行业大模型、零售行业大模型
  • VSCode 搭配 Windows 下各种 C/C++ 编译器使用
  • 【JavaEE】线程池和定时器
  • 《Unity3D网络游戏实战》通用服务器框架
  • LeetCode404 左叶子之和
  • nodejs操作redis的工具类
  • 关于wsl2与win11互联互通的问题
  • C++ 类型转换
  • 2024挖漏洞给报酬的网站汇总,兼职副业3天收益2k
  • 0到1学习Google广告(2):掌握展示位置及排名规则丨出海笔记
  • MySQL数据库读超时/SELECT查询超时 杂记
  • docker数据卷:
  • 【linux】linux中如何通过systemctl来创建和管理服务
  • WPF-实现多语言的静态(需重启)与动态切换(不用重启)
  • UE5学习笔记12-为角色添加蹲下的动作