当前位置:网站首页>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;
}
运行结果
边栏推荐
- 离线同步odps到mysql 中文乱码是因为?mysql已是utf8mb4
- VPC2187/8 电流模式 PWM 控制器 4-100VIN 超宽压启动、高度集成电源控制芯片推荐
- 通配符SSL证书不支持多域名吗?
- Google Earth Engine APP——一键在线查看全球1984-至今年的影像同时加载一个影像分析
- Understanding of margin collapse and coincidence
- DHCP&OSPF combined experimental demonstration (Huawei routing and switching equipment configuration)
- 悦刻难回巅峰
- 链表的经典入门LeetCode题目
- A group of friends asked for help, but the needs that were not solved in a week were solved in 3 minutes?
- PHP代码审计7—文件上传漏洞
猜你喜欢
The Industrial Metaverse Brings Changes to Industry
BigDecimal 使用注意!!“别踩坑”
解决错误:The package-lock.json file was created with an old version of npm
2019 Haidian District Youth Programming Challenge Activity Elementary Group Rematch Test Questions Detailed Answers
Matlab drawing 1
方法的重写
EasyCVR调用云端录像API接口返回错误且无录像文件生成,是什么原因?
2019年海淀区青少年程序设计挑战活动小学组复赛试题详细答案
PHP代码审计10—命令执行漏洞
(ECCV-2022)GaitEdge:超越普通的端到端步态识别,提高实用性
随机推荐
dotnet core 输出调试信息到 DebugView 软件
在表格数据集上训练变分自编码器 (VAE)示例
gbase8s创建RANGE分片表
ptables基本语法使用规则
Investigation and Research Based on the Involution Behavior of College Students
July 31, 2022 Summary of the third week of summer vacation
服务器
mood swings
buuctf(探险1)
如何给MySQL添加自定义语法 ?
LVS负载均衡群集之原理叙述
EasyCVR本地接入国标设备映射公网后,本地设备出现无法播放与级联的解决方法
部署LVS-DR群集
情绪的波动起伏
开发那些事儿:如何通过EasyCVR平台获取监控现场的人流量统计数据?
Go 言 Go 语,一文看懂 Go 语言文件操作
leetcode/有效的回文串,含有不需要判断回文的字符
报道称任天堂在2023年3月前不会推出任何新硬件产品
开篇-开启全新的.NET现代应用开发体验
Short-term reliability and economic evaluation of resilient microgrids under incentive-based demand response programs (Matlab code implementation)