IO学习

1.一个生产者对应多个消费者

cpp 复制代码
#include<myhead.h>

//创建条件变量
pthread_cond_t cond;

//创建一个互斥锁
pthread_mutex_t mutex;


//生产者
void *tast1(void *arg)
{
	int count=0;
	while(1)
	{
		if(count==3)
			break;
		sleep(1);
		printf("生产了一辆汽车\n");
		pthread_cond_signal(&cond);
		count++;
	}
	pthread_exit(NULL);
}

//消费者
void *tast2(void *arg)
{
	pthread_mutex_lock(&mutex);
	pthread_cond_wait(&cond,&mutex);
	printf("消费者购买了一辆车\n");
	pthread_mutex_unlock(&mutex);
	pthread_exit(NULL);
}

int main(int argc, const char *argv[])
{
	//初始化条件变量
	if(pthread_cond_init(&cond,NULL)!=0)
	{
		printf("pthread_cond_init error\n");
		return -1;
	}

	//初始化互斥锁
	if(pthread_mutex_init(&mutex,NULL)!=0)
	{
		printf("pthread_mutex_init\n");
		return -1;
	}

	//创建多个线程
	pthread_t tid[4];

	//生产者线程
	if(pthread_create(&tid[0],NULL,tast1,NULL)!=0)
	{
		printf("pthread_create error\n");
	}
	//消费者进程
	for(int i=1;i<4;i++)
	{
		if(pthread_create(&tid[i],NULL,tast2,NULL)!=0)
		{
			printf("pthread_create error\n");
			return -1;
		}
	}

	for(int i=0;i<4;i++)
		pthread_join(tid[i],NULL);
	return 0;
}

2使用多线程完成abc的顺序打印

cpp 复制代码
#include<myhead.h>


//定义一个无名信号
sem_t sem[3];

void *tast1(void *argc)
{
	sem_post(&sem[0]);
	int count=0;
	while(1)
	{
		if(count==5)
			break;
		sem_wait(&sem[0]);
		printf("a");
		sleep(1);
		fflush(stdout);
		sem_post(&sem[1]);
		count++;
	}
	pthread_exit(NULL);
}

void *tast2(void *argc)
{
	int count=0;
	while(1)
	{
		if(count==5)
			break;
		sem_wait(&sem[1]);
		printf("b");
		sleep(1);
		fflush(stdout);
		sem_post(&sem[2]);
		count++;
	}
	pthread_exit(NULL);
}

void *tast3(void *argc)
{
	int count=0;
	while(1)
	{
		if(count==5)
			break;
		sem_wait(&sem[2]);
		printf("c ");
		sleep(1);
		fflush(stdout);
		sem_post(&sem[0]);
		count++;
	}
	pthread_exit(NULL);
}

int main(int argc, const char *argv[])
{
	//初始化信号量
	for(int i=0;i<3;i++)
		sem_init(&sem[i],0,0);

	pthread_t pid1,pid2,pid3;
	if(pthread_create(&pid1,NULL,tast1,NULL)!=0)
	{
		printf("pthread_create error");
		return -1;
	}
	if(pthread_create(&pid2,NULL,tast2,NULL)!=0)
	{
		printf("pthread_create error");
		return -1;
	}
	if(pthread_create(&pid3,NULL,tast3,NULL)!=0)
	{
		printf("pthread_create error");
		return -1;
	}
	//回收线程资源
	pthread_join(pid1,NULL);
	pthread_join(pid2,NULL);
	pthread_join(pid3,NULL);

	//销毁无名信号量
	for(int i=0;i<3;i++)
	{
		if(sem_destroy(&sem[i])==-1)
		{
			perror("destroy error");
			return -1;
		}
	}

	return 0;
}
相关推荐
im_AMBER1 小时前
React 17
前端·javascript·笔记·学习·react.js·前端框架
谷歌开发者2 小时前
Web 开发指向标 | Chrome 开发者工具学习资源 (六)
前端·chrome·学习
QT 小鲜肉4 小时前
【QT/C++】Qt定时器QTimer类的实现方法详解(超详细)
开发语言·数据库·c++·笔记·qt·学习
Mr.Jessy5 小时前
Web APIs 学习第五天:日期对象与DOM节点
开发语言·前端·javascript·学习·html
存在morning5 小时前
【人工智能学习笔记 三】 AI教学之前端跨栈一:React整体分层架构
笔记·学习·架构
巫婆理发2225 小时前
评估指标+数据不匹配+贝叶斯最优误差(分析方差和偏差)+迁移学习+多任务学习+端到端深度学习
深度学习·学习·迁移学习
霜绛6 小时前
C#知识补充(二)——命名空间、泛型、委托和事件
开发语言·学习·unity·c#
好望角雾眠6 小时前
第四阶段C#通讯开发-6:Socket之UDP
开发语言·笔记·学习·udp·c#
_李小白7 小时前
【OPENGL ES 3.0 学习笔记】第十七天:模型矩阵、视图矩阵与投影矩阵
笔记·学习·矩阵
淮北4947 小时前
windows11配置wsl安装ubuntu20.04
windows·学习·ubuntu·wsl