IO线程day6

互斥锁的API

张三、李四取钱,临界资源为5000元

#include <myhead.h>
//1、定义互斥锁变量
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

int money=5000;   //临界资源

//创建线程体1
void *task1(void *arg)
{
	while(1)
	{
		sleep(1);

		//3、上锁
		pthread_mutex_lock(&mutex);

		if(money>=50)
		{
		money-=50;
		}

		printf("张三取的50元,剩余%d元\n",money);

		if(money<=0)
		{
			printf("没钱了,结束取款\n");
			//4、解锁,防止死锁
	//		pthread_mutex_unlock(&mutex);
			break;
		}

		//4、解锁,释放锁资源
		pthread_mutex_unlock(&mutex);
	}
	//退出线程
	pthread_exit(NULL);

}

//创建线程体2
void *task2(void *arg)
{
	while(1)
	{
		sleep(1);
		//3、上锁
		pthread_mutex_lock(&mutex);

		if(money>=100)
		{
		money-=100;
		}
		printf("李四取的100元,剩余%d元\n",money);

		if(money<=0)
		{
			printf("没钱了,结束取款\n");

			//4、解锁 ,否则会死锁
		//	pthread_mutex_unlock(&mutex);
			break;
		}
		//4、解锁
		pthread_mutex_unlock(&mutex);

	}
	//退出线程
	pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
	//定义线程号
	pthread_t tid1,tid2;

	//2、初始化互斥锁
	pthread_mutex_init(&mutex,NULL);

	//创建线程体
	if(pthread_create(&tid1,NULL,task1,NULL))
	{
		printf("线程1创建失败\n");
		return -1;
	}

	if(pthread_create(&tid2,NULL,task2,NULL))
	{
		printf("线程2创建失败\n");
		return -1;
	}

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

	//5、销毁锁
	pthread_mutex_destroy(&mutex);
		
	return 0;
}

如果不在break前解锁一次,在最后取完钱将不能正常退出

 正确运行结果:

 

 练习:使用无名信号量完成,定义三个线程,分别输出A、B、C;要求输出的顺序为ABCABCABCABCABC

#include <myhead.h>
//定义无名信号量
sem_t sem1,sem2,sem3;

//定义生产者线程

void *task1(void *arg)
{
	int num=5;
	while(num--)
	{
		sem_wait(&sem3);	

		printf("%#lx:A\n",pthread_self());

		//4、释放资源.
		sem_post(&sem1);
	}
	//退出线程
	pthread_exit(NULL);

}


//定义消费者线程
void *task2(void *arg)
{
	int num=5;
	while(num--)
	{
		//3、申请资源,如果没有资源,那么wait线程就会进入休眠态
		sem_wait(&sem1);

		printf("%#lx:B\n",pthread_self());

		//释放资源
		sem_post(&sem2);
	}
	//退出线程
	pthread_exit(NULL);
}


//定义消费者线程
void *task3(void *arg)
{
	int num=5;
	while(num--)
	{
		//3、申请资源
		sem_wait(&sem2);

		
		printf("%#lx:C\n",pthread_self());
		sem_post(&sem3);
	}
	pthread_exit(NULL);
}


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

	sem_init(&sem1,0,0);
	sem_init(&sem2,0,0);
	sem_init(&sem3,0,1);

	//创建三个线程
	if(pthread_create(&tid1,NULL,task1,NULL))
	{
		printf("创建失败");
		return -1;
	}

	if(pthread_create(&tid2,NULL,task2,NULL))
	{
		printf("创建失败");
		return -1;
	}
	if(pthread_create(&tid3,NULL,task3,NULL))
	{
		printf("线程3创建失败\n");
		return -1;
	}

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


	return 0;
}

 运行结果:

 

 条件变量

#include <myhead.h>
//1、定义条件变量
pthread_cond_t cond;

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

//创建生产者任务
void *task1(void *arg)
{
	sleep(1);
	printf("我生产了4辆叮叮车\n");
	pthread_cond_broadcast(&cond);

	//退出线程
	pthread_exit(NULL);

}
//创建消费者任务
void *task2(void *arg)
{
	//33、上锁
	pthread_mutex_lock(&mutex);

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

	printf("%#lx:我消费购买了一辆叮叮车\n",pthread_self());

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

	//退出线程
	pthread_exit(NULL);

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

	//2、初始化条件变量
	pthread_cond_init(&cond,NULL);

	//22、初始化互斥锁
	pthread_mutex_init(&mutex,NULL);

	//创建线程
	if(pthread_create(&tid1,NULL,task1,NULL))
	{
		printf("生产者创建失败\n");
		return -1;
	}

	if(pthread_create(&tid2,NULL,task2,NULL))
	{
		printf("消费者创建失败\n");
		return -1;
	}
	if(pthread_create(&tid3,NULL,task2,NULL))
	{
		printf("消费者创建失败\n");
		return -1;
	}
	if(pthread_create(&tid4,NULL,task2,NULL))
	{
		printf("消费者创建失败\n");
		return -1;
	}
	if(pthread_create(&tid5,NULL,task2,NULL))
	{
		printf("消费者创建失败\n");
		return -1;
	}

	printf("tid1=%#lx\ttid2=%#lx\ttid3=%#lx\ttid4=%#lx\ttid5=%#lx\n",tid1,tid2,tid3,tid4,tid5);

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

	//5、销毁条件变量
	pthread_cond_destroy(&cond);

	//55、销毁锁
	pthread_mutex_destroy(&mutex);

	return 0;
}

运行结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值