PlatformMutex
PlatformMutex 类层次结构
PlarformMutex 类在没有 RTOS 的情况下提供互斥存根函数。此类使您可以在 RTOS 不存在时使用驱动程序/应用程序代码。
PlatformMutex 类参考
公共成员函数 | |
void | lock () |
void | unlock () |
PlatformMutex 示例
下面的代码演示了 PlatformMutex 的用法。
#include "mbed.h"
PlatformMutex stdio_mutex;
Thread t2;
Thread t3;
void notify(const char* name, int state) {
stdio_mutex.lock();
printf("%s: %d\n\r", name, state);
stdio_mutex.unlock();
}
void test_thread(void const *args) {
while (true) {
notify((const char*)args, 0); wait(1);
notify((const char*)args, 1); wait(1);
}
}
int main() {
t2.start(callback(test_thread, (void *)"Th 2"));
t3.start(callback(test_thread, (void *)"Th 3"));
test_thread((void *)"Th 1");
}
对于所有驱动程序,Mbed OS 使用 PlatformMutex 类而不是 RTOS 互斥锁。例如,请参见 AnalogIn,BusOut,SPI,Serial 和 I2C。