当前位置:网站首页>IO thread process -> thread synchronization mutual exclusion mechanism -> day6
IO thread process -> thread synchronization mutual exclusion mechanism -> day6
2022-08-03 21:33:00 【Whale calls me to take care of the sea QAQ】
目录
作业
1.编写一个程序,开启3个 线程,这3个线程的ID分别为ABC,每个线程将自己的ID在屏幕上打印10 遍,要求输出结果必须按ABC的顺序显示,如ABCABC……依次递推;
一、按顺序打印
1.1 函数
#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <unistd.h>
int flag=0;
pthread_mutex_t mutex;
pthread_cond_t cond;
pthread_t tid1,tid2,tid3;
void* A(void *arg)
{
int i=0;
while(i<10)
{
pthread_mutex_lock(&mutex);
if(0 != flag)
{
pthread_cond_wait(&cond,&mutex);
}else
{
flag=1;
printf("A");
i++;
}
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
void* B(void* arg)
{
int i=0;
while(i<10)
{
pthread_mutex_lock(&mutex);
if(1 != flag)
{
pthread_cond_wait(&cond,&mutex);
}else
{
flag=2;
printf("B");
i++;
}
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
void* C(void* arg)
{
int i=0;
while(i<10)
{
pthread_mutex_lock(&mutex);
if(2 !=flag)
{
pthread_cond_wait(&cond,&mutex);
}else
{
flag=0;
printf("C\n");
i++;
}
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
if(pthread_mutex_init(&mutex,NULL) !=0)
{
perror("pthread_mutex_init");
return -1;
}
if(pthread_cond_init(&cond,NULL) != 0)
{
perror("pthread_cond_init");
return -1;
}
if(pthread_create(&tid1,NULL,A,NULL) != 0)
{
perror("pthread_create");
return -1;
}
if(pthread_create(&tid2,NULL,B,NULL) != 0)
{
perror("pthread_create");
return -1;
}
if(pthread_create(&tid3,NULL,C,NULL) != 0)
{
perror("pthread_create");
return -1;
}
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
pthread_join(tid3,NULL);
pthread_mutex_destroy(&mutex);
printf("\n");
return 0;
}//Cracked up today,不想写注释了,......
2.2 执行结果
二、倒置字符串
2.1 函数
#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <unistd.h>
char str[]="123456";
int flag=0;
pthread_mutex_t mutex;
pthread_cond_t cond;
pthread_t tid1,tid2;
void* dayin(void *arg)
{
while(1)
{
pthread_mutex_lock(&mutex);
if(1 != flag)
{
pthread_cond_wait(&cond,&mutex);
}
int len=strlen(str);
char temp;
for(int i=0;i<len/2;i++)
{
temp=str[i];
str[i]=str[len-1-i];
str[len-1-i]=temp;
}
flag=0;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
void* daizhi(void* arg)
{
while(1)
{
pthread_mutex_lock(&mutex);
if(0 != flag)
{
pthread_cond_wait(&cond,&mutex);
}
printf("%s\n",str);
flag=1;
sleep(1);
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
if(pthread_mutex_init(&mutex,NULL) !=0)
{
perror("pthread_mutex_init");
return -1;
}
if(pthread_cond_init(&cond,NULL) != 0)
{
perror("pthread_cond_init");
return -1;
}
if(pthread_create(&tid1,NULL,dayin,NULL) != 0)
{
perror("pthread_create");
return -1;
}
if(pthread_create(&tid2,NULL,daizhi,NULL) != 0)
{
perror("pthread_create");
return -1;
}
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
pthread_mutex_destroy(&mutex);
return 0;
}
2.2 执行结果
边栏推荐
猜你喜欢
基于支持向量机的网络⼊侵检测系统的全面调查和分类
业界新标杆!阿里开源自研高并发编程核心笔记(2022 最新版)
StoneDB 助力 2022 开放原子全球开源峰会
今晚直播 | 8.2-8.4 与你聊聊开源与就业那些事!
Diazo Biotin-PEG3-DBCO|重氮化合物修饰生物素-三聚乙二醇-二苯并环辛炔
反射机制
Zero trust, which has been popular for more than ten years, why can't it be implemented?
PyCharm function automatically add comments without parameters
idea2021.1.3版本如何启动多个客户端程序
DDD 中的几个困难问题
随机推荐
不专业面试官的经验总结
检测和控制影子IT的五个步骤
Orcad Capture Cadence 新建原理图多部分smybol和Homogeneous、Heterogeneous类型介绍教程
图神经网络怎么入门?一文带你了解图神经网络入门路径-GNN入门
业界新标杆!阿里开源自研高并发编程核心笔记(2022 最新版)
CAS:1260586-88-6_生物素-C5-叠氮_Biotin-C5-Azide
ES6--residual parameters
2022年全国职业院校技能大赛网络安全 B模块 任务十windows操作系统渗透测试 国赛原题
易基因|RNA m5C甲基化测序(RNA-BS)技术介绍
软件测试人员必备的60个测试工具清单,建议收藏一波~
6. XML
buildscript和allprojects的作用和区别是什么?
CheckBox列表项选中动画js特效
Five Steps to Detect and Control Shadow IT
DDD 中的几个困难问题
shell编程基础
XSS testing
【Odoo】硬核组件开发,全文没一句废话~
4. 模块化编程
CAS:153162-70-0_N-BOC-6-生物素酰氨基己胺