FreeRTOS 互斥锁最小示例
这是你在 FreeRTOS 中创建和使用互斥锁的方法:
包含:
freertos_includes.h
#include <freertos/semphr.h>全局声明
freertos_globals.cpp
SemaphoreHandle_t myMutex;初始化代码
在使用之前调用一次:
freertos_init.cpp
myMutex = xSemaphoreCreateMutex();如何锁定和解锁互斥锁
freertos_mutex_usage.cpp
// 最多等待 10ms 锁定互斥锁
if(xSemaphoreTake(myMutex, 10 / portTICK_PERIOD_MS) == pdTRUE) {
// 成功锁定互斥锁
// TODO:你的代码放在这里!
// 解锁互斥锁!
xSemaphoreGive(myMutex);
} else {
// 在超时时间内未能锁定互斥锁
// 不要使用受互斥锁保护的资源
// 不要解锁 (xSemaphoreGive)!
}Check out similar posts by category:
Embedded, FreeRTOS, PlatformIO
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow