当前位置:网站首页>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 执行结果
边栏推荐
猜你喜欢
随机推荐
Soft exam system analysts note experience sharing: theory of protracted war
剑指 Offer 07. 重建二叉树
距LiveVideoStackCon 2022 上海站开幕还有2天!
6. XML
CC2530_ZigBee+华为云IOT:设计一套属于自己的冷链采集系统
PyCharm function automatically add comments without parameters
dataframe multi-level index replace index df.swaplevel(axis=1)
系统运维系列 之CSV文件读取时内容中包含逗号的处理方法
B. Kalindrome Array
字节跳动软件测试岗,前两面过了,第三面HR天坑,结局透心凉...
关于GPIO你真的懂了吗?这篇文章都给你整理好了
今晚直播 | 8.2-8.4 与你聊聊开源与就业那些事!
在树莓派上搭建属于自己的网页(3)
易基因:植物宏病毒组研究:植物病毒的进化与生态 | 顶刊综述
七夕快乐!
[3D检测系列-PV-RCNN] PV-RCNN论文详解、PV-RCNN代码复现、包含官网PV-RCNN预训练权重及报错问题
IDaaS 是什么?一文说清它的价值
解决npm -v查看npm版本出现npm WARN config global `--global`, `--local` are deprecated. Use `--location报错
idea2021.1.3版本如何启动多个客户端程序
ES、Kibana 8.0安装