查看: 609|回复: 0
打印 上一主题 下一主题

【飞思卡尔MAPS四色板】09. mbed RTOS之Mutex操作

[复制链接] qrcode

31

主题

54

帖子

143

积分

注册会员

Rank: 2

积分
143
楼主
跳转到指定楼层
发表于 2016-4-30 11:00 AM | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

互斥信号量


当一个任务想对临界区访问时,为了防止别的任务也对该临界区操作,它需要对该临界区上锁,即take(获取)一个互斥的信号量,以保证独享。

验证代码:

#include "mbed.h"
#include "rtos.h"

DigitalOut led1(PTB7);
DigitalOut led2(PTB6);
DigitalOut led3(PTB5);
Mutex stdio_mutex;

void notify(const char* name, int state)
{
	stdio_mutex.lock();
	if((const char*)name == "LED1")
	{
		led1 = state;
	}
	if((const char*)name == "LED2")
	{
		led2 = state;
	}
	if((const char*)name == "LED3")
	{
		led3 = state;
	}
	wait(0.5);
	stdio_mutex.unlock();
}

void test_thread(void const *args)
{
	while (true)
	{
		notify((const char*)args, 0);
		Thread::wait(1000);
		notify((const char*)args, 1);
		Thread::wait(1000);
	}
}

int main()
{
	Thread t2(test_thread, (void *)"LED2");
	Thread t3(test_thread, (void *)"LED3");

	test_thread((void *)"LED1");
}
运行效果:

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表