互斥锁的初始值为1;
通过互斥锁保护临界区的代码执行;
1、初始化
osSemaphoreId_t sem_mutex;
int count = 0;
sem_mutex = osSemaphoreNew(1, 1, NULL);
2、线程1
osSemaphoreAcquire(sem_mutex, osWaitForever);
count++;
printf("%d\r\n", count);
osSemaphoreRelease(sem_mutex);
3、线程2
osSemaphoreAcquire(sem_mutex, osWaitForever);
count++;
printf("%d\r\n", count);
osSemaphoreRelease(sem_mutex);
4、结论
来源:CSDN
作者:小C菜鸟
链接:https://blog.csdn.net/C_cai_niao/article/details/103929518