当前位置:网站首页>Homework 8.3 Thread Synchronization Mutex Condition Variables
Homework 8.3 Thread Synchronization Mutex Condition Variables
2022-08-04 18:33:00 【Unknown college student 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>
//Define three condition variables
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[])
{
//Create three condition variables
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("Condition variable creation succeeded\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;
}
运行结果

边栏推荐
- powershell和cmd对比
- PHP代码审计10—命令执行漏洞
- After EasyCVR is locally connected to the national standard device to map the public network, the local device cannot play and cascade the solution
- Kubernetes入门到精通- Operator 模式入门
- 路由技术
- ”元宇宙“必须具备这些特点
- 路由懒加载
- 机器学习——线性回归
- 谁能解答?从mysql的binlog读取数据到kafka,但是数据类型有Insert,updata,
- 面试官:MVCC是如何实现的?
猜你喜欢

使用scikit-learn计算文本TF-IDF值

After EasyCVR is locally connected to the national standard device to map the public network, the local device cannot play and cascade the solution

当项目中自动格式化插件Prettier和ESLint冲突报错时如何解决

YOLOv7-Pose尝鲜,基于YOLOv7的关键点模型测评

路由技术

部署LVS-DR群集

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

力扣学习---0804

入选爱分析·银行数字化厂商全景报告,网易数帆助力金融数字化场景落地

npm配置国内镜像(淘宝镜像)
随机推荐
使用.NET简单实现一个Redis的高性能克隆版(二)
当项目中自动格式化插件Prettier和ESLint冲突报错时如何解决
【杰神说说】物联大师2.0版本预告
Documentary on Security Reinforcement of Network Range Monitoring System (1)—SSL/TLS Encrypted Transmission of Log Data
curl命令的那些事
MMDetection 使用示例:从入门到出门
EasyCVR调用云端录像API接口返回错误且无录像文件生成,是什么原因?
Route lazy loading
【web自动化测试】Playwright快速入门,5分钟上手
Go 言 Go 语,一文看懂 Go 语言文件操作
PHP代码审计9—代码执行漏洞
2018读书记
开篇-开启全新的.NET现代应用开发体验
DHCP&OSPF combined experimental demonstration (Huawei routing and switching equipment configuration)
Flink / Scala - 使用 RedisSink 存储数据
DHCP&OSPF组合实验演示(Huawei路由交换设备配置)
入选爱分析·银行数字化厂商全景报告,网易数帆助力金融数字化场景落地
不论你是大众,科班和非科班,我这边整理很久,总结出的学习路线,还不快卷起来
ptables基本语法使用规则
linux下Mysql的简单操作