当前位置:网站首页>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 执行结果
边栏推荐
- 《强化学习周刊》第56期:GraphIRL、REDEEMER & 眼科强化学习的潜在研究
- 易基因|RNA m5C甲基化测序(RNA-BS)技术介绍
- buildscript和allprojects的作用和区别是什么?
- Soft exam system analysts note experience sharing: theory of protracted war
- ES、Kibana 8.0安装
- 如何使用 Jmeter获取登录token并设置为全局变量?
- 编译器工程师眼中的好代码(1):Loop Interchange
- How to deal with commas in the content of the CSV file of the system operation and maintenance series
- 用 setTimeout 来实现 setInterval
- AI首席架构师13-AICA-智能文档分析技术在行业场景中的应用
猜你喜欢
From September 1st, my country has granted zero-tariff treatment to 98% of tax items from 16 countries including Togo
什么密码,永远无法被黑客攻破?
STP生成树
开源一夏 |如何优化线上服务器
3种圆形按钮悬浮和点击事件
现网设备兼容SRv6网络演进
深度学习怎么入门?零基础快速入门深度学习
【使用 Pytorch 实现入门级的人工神经网络】
XSS练习---一次循环和两次循环问题
[kali-vulnerability scanning] (2.1) Nessus download and installation (on)
随机推荐
什么密码,永远无法被黑客攻破?
东西向和南北向通信的统一
检测和控制影子IT的五个步骤
STP生成树
聚焦开源与联合共创|麒麟软件出席开源峰会欧拉分论坛
不专业面试官的经验总结
XSS线上靶场---prompt
LitJson报错记录
服务器安装redis
ValidationError: Progress Plugin Invalid Options
4. 模块化编程
15 years experience in software architect summary: in the field of ML, tread beginners, five hole
反射机制
Cesium 修改鼠标样式
Zero trust, which has been popular for more than ten years, why can't it be implemented?
研究Fabric中Etcd的Raft应用
尚医通项目总结
好朋友离职了,一周面试了20多场,我直呼内行
剑指 Offer 07. 重建二叉树
ES6--residual parameters