嵌入式 IO day6 互斥锁、无名信号量、条件变量

1.互斥锁:

#include <myhead.h>

int num=5000;//全局变量 临界资源
//1.定义一个锁资源
pthread_mutex_t mutex;

void *task1(void *arg)
{
	while(1){
	//3.上锁
	pthread_mutex_lock(&mutex);

	num=num-100;
	printf("张三取了100元,还剩%d\n",num);

	if(num<=0)
	{
		printf("张三取钱结束,因为钱不够了\n");
		//释放锁资源
		pthread_mutex_unlock(&mutex);

		break;
	}

	//释放锁资源
	pthread_mutex_unlock(&mutex);
	sleep(1);
	}

	//退出线程
	pthread_exit(NULL);
}

void *task2(void *arg)
{
	while(1)
	{
	//3.给临界区上锁
	pthread_mutex_lock(&mutex);

	num=num-50;
	printf("李四取了50元,还剩%d\n",num);

	if(num<=0)
	{
		printf("李四取钱结束,因为钱不够了\n");
		//当循环结束时,也要释放锁资源
		pthread_mutex_unlock(&mutex);

		break;
	}

	//4.释放锁资源
	pthread_mutex_unlock(&mutex);

	sleep(1);
	}

	//退出线程
	pthread_exit(NULL);
}

int main(int argc, const char *argv[])
{
	pthread_t tid1,tid2;

	if(pthread_create(&tid1,NULL,task1,NULL))
	{
		perror("create tid1 error");
		return -1;
	}

	if(pthread_create(&tid2,NULL,task2,NULL))
	{
		perror("create tid2 error");
		return -1;
	}
	
	//2.初始化互斥锁
	pthread_mutex_init(&mutex,NULL);

	//回收线程资源
	pthread_join(tid1,NULL);
	pthread_join(tid2,NULL);

	//5.销毁锁资源
	pthread_mutex_destroy(&mutex);

	return 0;
}

运行结果; 

 

 

2.无名信号量:

#include <head.h>
//1、定义了一个无名信号量
sem_t sema;
sem_t semb;
sem_t semc;

//定义生产者流程
void *task1(void *arg)
{
	while(1)
	{
		sleep(1);
		sem_wait(&semc);
		printf("a");
		fflush(stdout);
		//4、释放资源
		sem_post(&sema);
	}
}
void *task2(void *arg)
{
	while(1)
	{
		sleep(1);
		//3、申请资源
		sem_wait(&sema);  //如果没有资源可以申请,则等待
		printf("b");
		fflush(stdout);
		sem_post(&semb);
	}
}
void *task3(void *arg)
{
	while(1)
	{
		sleep(1);
		sem_wait(&semb);
		printf("c");
		fflush(stdout);
		sem_post(&semc);

	}
}
int main(int argc, const char *argv[])
{
	pthread_t tid1,tid2,tid3;
	//创建两个线程
	if(pthread_create(&tid1,NULL,task1,NULL))
	{
		perror("create tid1 error");
		return -1;
	}
	if(pthread_create(&tid2,NULL,task2,NULL))
	{
		perror("create tid2 error");
		return -1;
	}
	if(pthread_create(&tid3,NULL,task3,NULL))
	{
		perror("create tid3 error");
		return -1;
	}

	//2、初始化无名信号量
	sem_init(&sema,0,0);        //第一个0表示该无名信号量用于线程之间,第二个0表示初始值时,没有资源可以申请
	sem_init(&semb,0,0);        //第一个0表示该无名信号量用于线程之间,第二个0表示初始值时,没有资源可以申请
	sem_init(&semc,0,1);        //第一个0表示该无名信号量用于线程之间,第二个0表示初始值时,没有资源可以申请

	//阻塞等待回收子线程的资源
	pthread_join(tid1,NULL);
	pthread_join(tid2,NULL);
	pthread_join(tid3,NULL);

	//5、销毁无名信号量
	sem_destroy(&sema);
	sem_destroy(&semb);
	sem_destroy(&semc);
	return 0;
}

运行结果:

 3.条件变量:

#include<myhead.h>

//1、定义条件变量
pthread_cond_t cond;

//11、定义互斥锁
pthread_mutex_t mutex;

//创建生产者线程
void *task1(void *arg)
{
	while(1)
	{
		sleep(3);

		printf("我生产了三辆跑车\n");
		//3、释放资源,并通知队列中的等待线程
		//pthread_cond_signal(&cond);    //唤醒第一个等待线程
		pthread_cond_broadcast(&cond);    //唤醒所有等待线程
	}
}

//创建消费者线程
void *task2(void *arg)
{
	while(1)
	{
		sleep(1);
		
		//33、上锁
		pthread_mutex_lock(&mutex);

		//4、申请资源
		pthread_cond_wait(&cond, &mutex);

		printf("%#lx:我消费一个跑车\n", pthread_self());

		//44、解锁
		pthread_mutex_unlock(&mutex);
	}
}

int main(int argc, const char *argv[])
{
	//定义四个线程
	pthread_t tid1, tid2, tid3, tid4;

	//创建四个线程
	if(pthread_create(&tid1, NULL, task1, NULL))
	{
		perror("create tid1 error");
		return -1;
	}

	if(pthread_create(&tid2, NULL, task2, NULL))
	{
		perror("create tid2 error");
		return -1;
	}
	
	if(pthread_create(&tid3, NULL, task2, NULL))
	{
		perror("create tid3 error");
		return -1;
	}

	if(pthread_create(&tid4, NULL, task2, NULL))
	{
		perror("create tid4 error");
		return -1;
	}

	//2、初始化一个条件变量
	pthread_cond_init(&cond, NULL);
	//22、初始化互斥锁
	pthread_mutex_init(&mutex,NULL);

	printf("tid1=%#lx,tid2=%#lx,tid3=%#lx,tid4=%#lx",\
			tid1,tid2,tid3,tid4);



	//回收线程资源
	pthread_join(tid1, NULL);
	pthread_join(tid2, NULL);
	pthread_join(tid3, NULL);
	pthread_join(tid4, NULL);

	//5、释放条件变量
	pthread_cond_destroy(&cond);
	//55、释放锁资源
	pthread_mutex_destroy(&mutex);


	return 0;
}

运行结果: 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值