当前位置:网站首页>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 执行结果
边栏推荐
- D - Project Planning--二分
- 有趣的opencv-记录图片二值化和相似度实现
- 函数,递归以及dom简单操作
- 聚焦开源与联合共创|麒麟软件出席开源峰会欧拉分论坛
- Transformer怎么入门?如何学习Transformer?
- Cesium 修改鼠标样式
- idea2021配置svn报错Cannot run program “svn“ (in directory “xxx“):CreateProcess error=2,系统找不到指定的文件
- dataframe multi-level index replace index df.swaplevel(axis=1)
- IO线程进程->线程同步互斥机制->day6
- Interesting opencv - record image binarization and similarity
猜你喜欢
随机推荐
小朋友学C语言(3):整数、浮点数、字符
buildscript和allprojects的作用和区别是什么?
FVCOM三维水动力、水交换、溢油物质扩散及输运数值模拟丨FVCOM模型流域、海洋水环境数值模拟方法
XSS practice - cycle and two cycle problem at a time
[kali-vulnerability exploitation] (3.2) Metasploit basics (on): basic knowledge
svg+js订单确认按钮动画js特效
CAS:153162-70-0_N-BOC-6-生物素酰氨基己胺
A. Color the Picture- Codeforces Round #810 (Div. 1)
CAS:1260586-88-6_生物素-C5-叠氮_Biotin-C5-Azide
易基因|RNA m5C甲基化测序(RNA-BS)技术介绍
Security Fundamentals 8 --- XSS
Diazo Biotin-PEG3-DBCO|重氮化合物修饰生物素-三聚乙二醇-二苯并环辛炔
LyScript 实现应用层钩子扫描器
dataframe 多层索引 更换索引 df.swaplevel(axis=1)
LVS负载均衡集群
CAS: 773888-45-2_BIOTIN ALKYNE_生物素-炔基
E - Swap
七夕快乐!
详解虚拟机!京东大佬出品 HotSpot VM 源码剖析笔记(附完整源码)
解决npm -v查看npm版本出现npm WARN config global `--global`, `--local` are deprecated. Use `--location报错