当前位置: 首页 > news >正文

IO进程线程:通信

1.定义互斥锁

#include<myhead.h>int num=520;//临界资源//1.创建一个互斥锁变量
pthread_mutex_t mutex;//定义任务1函数
void *task1(void *arg)
{printf("11111111111111\n");//3.获取锁资源pthread_mutex_lock(&mutex);num=1314;sleep(3);printf("task1:num=%d\n",num);//释放锁资源pthread_mutex_unlock(&mutex);
}//定义任务2函数
void *task2(void *arg)
{printf("22222222222222\n");//3.获取锁资源pthread_mutex_lock(&mutex);num++;sleep(1);//在休眠时,任务1执行到赋值语句printf("task2:num=%d\n",num);//释放锁资源pthread_mutex_unlock(&mutex);
}
int main(int argc, const char *argv[])
{//2.初始化互斥锁pthread_mutex_init(&mutex,NULL);//创建两个线程pthread_t tid1,tid2;if(pthread_create(&tid1,NULL,task1,NULL)!=0){printf("tid1 create error\n");return -1;}if(pthread_create(&tid2,NULL,task2,NULL)!=0){printf("tid2 create error\n");return -1;}printf("tid1:%#lx,tid2:%#lx\n",tid1,tid2);//回收线程资源pthread_join(tid1,NULL);pthread_join(tid2,NULL);//5.销毁锁资源pthread_mutex_destroy(&mutex);return 0;
}

2.定义无名信号量

#include<myhead.h>
//1.创建无名信号量
sem_t sem;//定义生产者线程
void *task1(void *arg)
{int num=5;while(num--){sleep(1);printf("我生产了一辆特斯拉\n");//4.释放资源sem_post(&sem);}pthread_exit(NULL);//退出线程
}//定义消费者线程
void *task2(void *arg)
{int num=5;while(num--){//3.申请资源sem_wait(&sem);printf("我消费了一辆特斯拉\n");}pthread_exit(NULL);//退出线程
}
int main(int argc, const char *argv[])
{//2.初始化无名信号量sem_init(&sem,0,0);//第一个0表示用于线程同步,第二个0表示初始资源为0//创建两个线程,分别是生产者和消费者pthread_t tid1,tid2;if(pthread_create(&tid1,NULL,task1,NULL)!=0){printf("tid1 create error\n");return -1;}if(pthread_create(&tid2,NULL,task2,NULL)!=0){printf("tid2 create error\n");return -1;}printf("tid1:%#lx,tid2:%#lx\n",tid1,tid2);//回收线程资源pthread_join(tid1,NULL);pthread_join(tid2,NULL);//释放无名信号量sem_destroy(&sem);return 0;
}

3.创建三个线程,线程1打印输出字符A,线程2打印输出字符B,线程3打印输出字符C,用无名信号量输出结果为ABCABCABCABCABC。

#include<myhead.h>
//创建无名信号量
sem_t sem1,sem2,sem3;//定义线程1
void *task1(void *arg)
{int num=5;while(num--){sem_wait(&sem1);sleep(1);printf("A");fflush(stdout);sem_post(&sem2);}pthread_exit(NULL);
}
//定义线程2
void *task2(void *arg)
{int num=5;while(num--){sem_wait(&sem2);sleep(1);printf("B");fflush(stdout);sem_post(&sem3);}pthread_exit(NULL);
}
//定义线程3
void *task3(void *arg)
{int num=5;while(num--){sem_wait(&sem3);sleep(1);printf("C");fflush(stdout);sem_post(&sem1);}pthread_exit(NULL);
}int main(int argc, const char *argv[])
{sem_init(&sem1,0,1);sem_init(&sem2,0,0);sem_init(&sem3,0,0);pthread_t tid1,tid2,tid3;if(pthread_create(&tid1,NULL,task1,NULL)!=0){printf("tid1 create error\n");return -1;}if(pthread_create(&tid2,NULL,task2,NULL)!=0){printf("tid2 create error\n");return -1;}if(pthread_create(&tid3,NULL,task3,NULL)!=0){printf("tid3 create error\n");return -1;}printf("tid1:%#lx,tid2:%#lx,tid3:%#lx\n",tid1,tid2,tid3);pthread_join(tid1,NULL);pthread_join(tid2,NULL);pthread_join(tid3,NULL);sem_destroy(&sem1);sem_destroy(&sem2);sem_destroy(&sem3);puts("");return 0;
}

4.线程同步之条件变量

#include<myhead.h>
//1.定义条件变量
pthread_cond_t cond;//11.定义互斥锁变量
pthread_mutex_t mutex;//定义生产者线程1
void *task1(void *arg)
{int num=5;while(num--){sleep(1);printf("%#lx:生产了一辆特斯拉\n",pthread_self());//3.唤醒一个消费者//pthread_cond_signal(&cond);}//3.唤醒所有的等待线程pthread_cond_broadcast(&cond);//退出线程pthread_exit(NULL);
}
//定义消费者线程2
void *task2(void *arg)
{//33.上锁pthread_mutex_lock(&mutex);//4.进入等待队列pthread_cond_wait(&cond,&mutex);printf("%#lx:消费了一辆特斯拉\n",pthread_self());//44.解锁pthread_mutex_unlock(&mutex);//退出线程pthread_exit(NULL);
}
//定义消费者线程3
void *task3(void *arg)
{//33.上锁pthread_mutex_lock(&mutex);//4.进入等待队列pthread_cond_wait(&cond,&mutex);printf("%#lx:消费了一辆特斯拉\n",pthread_self());//44.解锁pthread_mutex_unlock(&mutex);//退出线程pthread_exit(NULL);
}
//定义消费者线程4
void *task4(void *arg)
{//33.上锁pthread_mutex_lock(&mutex);//4.进入等待队列pthread_cond_wait(&cond,&mutex);printf("%#lx:消费了一辆特斯拉\n",pthread_self());//44.解锁pthread_mutex_unlock(&mutex);//退出线程pthread_exit(NULL);
}
//定义消费者线程5
void *task5(void *arg)
{//33.上锁pthread_mutex_lock(&mutex);//4.进入等待队列pthread_cond_wait(&cond,&mutex);printf("%#lx:消费了一辆特斯拉\n",pthread_self());//44.解锁pthread_mutex_unlock(&mutex);//退出线程pthread_exit(NULL);
}
//定义消费者线程6
void *task6(void *arg)
{//33.上锁pthread_mutex_lock(&mutex);//4.进入等待队列pthread_cond_wait(&cond,&mutex);printf("%#lx:消费了一辆特斯拉\n",pthread_self());//44.解锁pthread_mutex_unlock(&mutex);//退出线程pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{//2.初始化条件变量pthread_cond_init(&cond,NULL);//22.初始化互斥锁pthread_mutex_init(&mutex,NULL);//创建六个线程,一个生产者,五个消费者pthread_t tid1,tid2,tid3,tid4,tid5,tid6;if(pthread_create(&tid1,NULL,task1,NULL)!=0){printf("tid1 create error\n");return -1;}if(pthread_create(&tid2,NULL,task2,NULL)!=0){printf("tid2 create error\n");return -1;}	if(pthread_create(&tid3,NULL,task3,NULL)!=0){printf("tid3 create error\n");return -1;}if(pthread_create(&tid4,NULL,task4,NULL)!=0){printf("tid4 create error\n");return -1;}if(pthread_create(&tid5,NULL,task5,NULL)!=0){printf("tid5 create error\n");return -1;}if(pthread_create(&tid6,NULL,task6,NULL)!=0){printf("tid6 create error\n");return -1;}printf("tid1:%#lx,tid2:%#lx,tid3:%#lx,tid4:%#lx,tid5:%#lx5,tid6:%#lx\n",tid1,tid2,tid3,tid4,tid5,tid6);//回收线程资源pthread_join(tid1,NULL);pthread_join(tid2,NULL);pthread_join(tid3,NULL);pthread_join(tid4,NULL);pthread_join(tid5,NULL);pthread_join(tid6,NULL);//5.销毁条件变量pthread_cond_destroy(&cond);//55.销毁互斥锁pthread_mutex_destroy(&mutex);return 0;
}

http://www.lryc.cn/news/305715.html

相关文章:

  • 神经网络系列---常用梯度下降算法
  • Flink 的历史版本特性介绍(一)
  • 【尚硅谷】MybatisPlus 学习笔记(下)
  • 408数据结构算法模板
  • Mysql--索引分类
  • AutoTimes:通过大语言模型的自回归时间序列预测器
  • 记录 | go与C/C++交互
  • B3623枚举排列
  • vuex怎么防止数据刷新丢失?
  • OpenGL ES 渲染 NV21、NV12、I420、YV12、YUYV、UYVY、I444(建议收藏)
  • 云计算的两地三中心和灾备介绍
  • Spring Bean
  • Linux的时间操作
  • 2024-02-21 作业
  • 平台组成-监控服务
  • 探索分布式强一致性奥秘:Paxos共识算法的精妙之旅
  • 使用 ES|QL 优化可观察性:简化 Kubernetes 和 OTel 的 SRE 操作和问题解决
  • Docker 第十九章 : 阿里云个人镜像仓使用
  • 二、系统知识笔记-系统架构概述
  • 【高德地图】Android高德地图绘制标记点Marker
  • 每天一个知识点 - 如何快速熟悉后端项目
  • 如何将cocos2d-x js打包部署到ios上 Mac M1系统
  • pdffactory pro 8中文破解版
  • 常用ADB命令整理已经ADB键盘输入
  • buuctf_N1BOOK_粗心的小李
  • 爬取链家二手房房价数据存入mongodb并进行分析
  • 论文阅读:Ground-Fusion: A Low-cost Ground SLAM System Robust to Corner Cases
  • 一键获取电商平台商品信息,快速提高电商业务效率
  • vue 中实现音视频播放进度条(满足常见开发需求)
  • 【广度优先搜索】【网格】【割点】1263. 推箱子