当前位置:网站首页>作业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;
}
运行结果
边栏推荐
- 老电脑怎么重装系统win10
- 谁能解答?从mysql的binlog读取数据到kafka,但是数据类型有Insert,updata,
- Google Earth Engine APP——一键在线查看全球1984-至今年的影像同时加载一个影像分析
- flink-cdc支持并行读取一张mysql表的binlog不?
- 【web自动化测试】Playwright快速入门,5分钟上手
- EasyCVR调用云端录像API接口返回错误且无录像文件生成,是什么原因?
- 力扣学习---0804
- 不论你是大众,科班和非科班,我这边整理很久,总结出的学习路线,还不快卷起来
- dotnet core 使用 CoreRT 将程序编译为 Native 程序
- 数据库SqlServer迁移PostgreSql实践
猜你喜欢
部署LVS-DR群集
Flask框架实现注册加密功能详解【Flask企业课学习】
EasyCVR calls the cloud recording API and returns an error and no recording file is generated. What is the reason?
开发那些事儿:如何通过EasyCVR平台获取监控现场的人流量统计数据?
从-99打造Sentinel高可用集群限流中间件
2019 Haidian District Youth Programming Challenge Activity Elementary Group Rematch Test Questions Detailed Answers
使用.NET简单实现一个Redis的高性能克隆版(二)
自己经常使用的三种调试:Pycharm、Vscode、pdb调试
leetcode 13. 罗马数字转整数
阿里云技术专家秦隆:云上如何进行混沌工程?
随机推荐
面试官:MVCC是如何实现的?
linux下Mysql的简单操作
gbase8s创建RANGE分片表
谁能解答?从mysql的binlog读取数据到kafka,但是数据类型有Insert,updata,
实验室专利书写指南
基于 eBPF 的 Kubernetes 可观测实践
【web自动化测试】playwright安装失败怎么办
EasyCVR如何通过接口调用设备录像的倍速回放?
Error when using sourcemap for reporting an error: Can‘t resolve original location of error.
自己经常使用的三种调试:Pycharm、Vscode、pdb调试
Route lazy loading
darknet source code reading notes-02-list.h and lish.c
clickhouse online and offline table
Alibaba Cloud International Edition uses ROS to build WordPress tutorial
Investigation and Research Based on the Involution Behavior of College Students
如何给MySQL添加自定义语法 ?
leetcode 14. 最长公共前缀
2018读书记
图解LeetCode——899. 有序队列(难度:困难)
基于激励的需求响应计划下弹性微电网的短期可靠性和经济性评估(Matlab代码实现)