当前位置:网站首页>IO线程进程->线程同步互斥机制->day6
IO线程进程->线程同步互斥机制->day6
2022-08-03 21:13:00 【鲸叫我照顾海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;
}//今天裂开了,不想写注释了,。。。。。。
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 执行结果

边栏推荐
猜你喜欢

Markdown syntax

用 setTimeout 来实现 setInterval

canvas螺旋动画js特效

2022年全国职业院校技能大赛网络安全 B模块 任务十windows操作系统渗透测试 国赛原题

Use setTimeout to realize setInterval

461. 汉明距离

【kali-漏洞利用】(3.2)Metasploit基础(上):基础知识
![[kali-vulnerability exploitation] (3.2) Metasploit basics (on): basic knowledge](/img/49/117de5147a34e6a957f74880b4f597.png)
[kali-vulnerability exploitation] (3.2) Metasploit basics (on): basic knowledge

【使用 Pytorch 实现入门级的人工神经网络】

ES、Kibana 8.0安装
随机推荐
不专业面试官的经验总结
StoneDB 开源社区月刊 | 202207期
XSS online shooting range---prompt
CheckBox列表项选中动画js特效
2022年全国职业院校技能大赛网络安全 B模块 任务十windows操作系统渗透测试 国赛原题
chartjs自定义柱状图插件
2022年强网杯rcefile wp
卷起来!阿里高工携 18 位高级架构师耗时 57 天整合的 1658 页面试总结
C. Fishingprince Plays With Array--Codeforces Global Round 21
XSS online shooting range---Warmups
主板设计中:网络变压器与RJ45网口之间应该保持什么样的距离?
函数,递归以及dom简单操作
[3D检测系列-PV-RCNN] PV-RCNN论文详解、PV-RCNN代码复现、包含官网PV-RCNN预训练权重及报错问题
软考系统分析师备考经验分享:论持久战
XSS线上靶场---Warmups
XSS testing
Five Steps to Detect and Control Shadow IT
系统运维系列 之CSV文件读取时内容中包含逗号的处理方法
15 years experience in software architect summary: in the field of ML, tread beginners, five hole
Often forget HiFlow 】 【 check-in?Use tencent cloud scenario connector to remind you every day.