当前位置:网站首页>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;
}
运行结果

边栏推荐
猜你喜欢

A group of friends asked for help, but the needs that were not solved in a week were solved in 3 minutes?

运力升级助力算力流转,中国数字经济的加速时刻

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

npm配置国内镜像(淘宝镜像)

报道称任天堂在2023年3月前不会推出任何新硬件产品

curl命令的那些事

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

基于激励的需求响应计划下弹性微电网的短期可靠性和经济性评估(Matlab代码实现)

火灾报警联网FC18中CAN光端机常见问题解答和使用指导

Investigation and Research Based on the Involution Behavior of College Students
随机推荐
关于使用腾讯云HiFlow场景连接器每天提醒签到打卡
Boosting之GBDT原理
Understanding of margin collapse and coincidence
部署LVS-DR群集
localstorage本地存储的方法
ERC721标准与加密猫
Error when using sourcemap for reporting an error: Can‘t resolve original location of error.
【web自动化测试】Playwright快速入门,5分钟上手
MMDetection 使用示例:从入门到出门
阿里云技术专家秦隆:云上如何进行混沌工程?
基于 eBPF 的 Kubernetes 可观测实践
MySQL安装教程(详细)
面试官:MVCC是如何实现的?
DHCP&OSPF combined experimental demonstration (Huawei routing and switching equipment configuration)
How does the intelligent video surveillance platform EasyCVR use the interface to export iframe addresses in batches?
作业8.3 线程同步互斥机制条件变量
巴比特 | 元宇宙每日必读:微博动漫将招募全球各类虚拟偶像并为其提供扶持...
如何封装 svg
Develop those things: How to obtain the traffic statistics of the monitoring site through the EasyCVR platform?
ptables基本语法使用规则