当前位置:网站首页>作业8.3 线程同步互斥机制条件变量
作业8.3 线程同步互斥机制条件变量
2022-08-04 18:24:00 【不知名大学生M】
题目:编写一个程序,开启3个 线程,这3个线程的ID分别为ABC,每个线程将自己的ID在屏幕上打印10遍,要求输出结果必须按ABC的顺序显示,如ABCABC……依次递推;
提示:A只能叫醒B,B只能唤醒C,C只能唤醒A;
实现代码
#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <semaphore.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
//定义三个条件变量
pthread_cond_t cond1;
pthread_cond_t cond2;
pthread_cond_t cond3;
//互斥锁
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
void* A(void* arg)
{
for(int i=0;i<10;i++)
{
pthread_mutex_lock(&mutex);
printf("A");
pthread_cond_signal(&cond2);
pthread_cond_wait(&cond1, &mutex);
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
void* B(void* arg)
{
for(int i=0;i<10;i++)
{
pthread_mutex_lock(&mutex);
pthread_cond_wait(&cond2, &mutex);
printf("B");
pthread_cond_signal(&cond3);
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
void* C(void* arg)
{
for(int i=0;i<10;i++)
{
pthread_mutex_lock(&mutex);
pthread_cond_wait(&cond3, &mutex);
printf("C");
pthread_cond_signal(&cond1);
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
//创建三个条件变量
if(pthread_cond_init(&cond1, NULL) != 0)
{
perror("pthread_cond_init");
return -1;
}
if(pthread_cond_init(&cond2, NULL) != 0)
{
perror("pthread_cond_init");
return -1;
}
if(pthread_cond_init(&cond3, NULL) != 0)
{
perror("pthread_cond_init");
return -1;
}
printf("创建条件变量成功\n");
//创建3个线程
pthread_t tid1,tid2,tid3;
//A线程打印A
if(pthread_create(&tid1,NULL,A,NULL)!=0)
{
perror("pthread_creat");
return -1;
}
//B线程打印B
if(pthread_create(&tid2,NULL,B,NULL)!=0)
{
perror("pthread_creat");
return -1;
}
//C线程打印C
if(pthread_create(&tid3,NULL,C,NULL)!=0)
{
perror("pthread_creat");
return -1;
}
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
pthread_join(tid3,NULL);
printf("\n");
//销毁互斥锁
pthread_mutex_destroy(&mutex);
//销毁条件变量
pthread_cond_destroy(&cond1);
pthread_cond_destroy(&cond2);
pthread_cond_destroy(&cond3);
return 0;
}
运行结果

边栏推荐
猜你喜欢

路由技术

EasyCVR calls the cloud recording API and returns an error and no recording file is generated. What is the reason?

Google AppSheet: 无需编程构建零代码应用

网站设计师:Nicepage 4.15 Crack By Xacker

Flink/Scala - Storing data with RedisSink

(ECCV-2022)GaitEdge:超越普通的端到端步态识别,提高实用性

EasyCVR调用云端录像API接口返回错误且无录像文件生成,是什么原因?

【杰神说说】物联大师2.0版本预告

Babbitt | Metaverse daily must-read: Weibo animation will recruit all kinds of virtual idols around the world and provide support for them...

Flask framework implementations registered encryption, a Flask enterprise class learning 】 【
随机推荐
Google Earth Engine APP——一键在线查看全球1984-至今年的影像同时加载一个影像分析
2018读书记
开发那些事儿:如何通过EasyCVR平台获取监控现场的人流量统计数据?
通配符SSL证书不支持多域名吗?
数仓相关,总结
golang安装和基础配置
用Excel绘制统计图
ERC721标准与加密猫
单行、多行文本超出显示省略号
阿里云国际版使用ROS搭建WordPress教程
Nintendo won't launch any new hardware until March 2023, report says
如何封装 svg
不论你是大众,科班和非科班,我这边整理很久,总结出的学习路线,还不快卷起来
EasyCVR调用云端录像API接口返回错误且无录像文件生成,是什么原因?
动态数组底层是如何实现的
哈夫曼树(暑假每日一题 15)
flink-cdc支持并行读取一张mysql表的binlog不?
Interval greedy (interval merge)
Regardless of whether you are a public, professional or non-major class, I have been sorting out the learning route for a long time here, and the learning route I have summarized is not yet rolled up
Global electronics demand slows: Samsung's Vietnam plant significantly reduces capacity