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.mzph.cn/news/701796.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

EasyRecovery 16数据恢复软件功能介绍及2024 年最新easyrecover激活密钥?

EasyRecovery Photo16 for windows数据恢复软件免费版下载是一款由Kroll Ontrack公司开发的数据恢复软件&#xff0c;其主要功能是恢复已经删除或损坏的图片文件。该软件可用于恢复各种类型的图片文件&#xff0c;包括JPEG、GIF、BMP、PNG等&#xff0c;同时也支持恢复照片文件…

python-pyecharts画饼图

pyecharts饼图 from pyecharts import options as opts from pyecharts.charts import Pie# 构造数据 data [("A", 10),("B", 20),("C", 30),("D", 40),("E", 50) ]# 实例化饼图 pie Pie()# 添加数据 pie.add("&qu…

【Java多线程】对线程池的理解并模拟实现线程池

目录 1、池 1.1、线程池 2、ThreadPoolExecutor 线程池类 3、Executors 工厂类 4、模拟实现线程池 1、池 “池”这个概念见到非常多&#xff0c;例如常量池、数据库连接池、线程池、进程池、内存池。 所谓“池”的概念就是&#xff1a;&#xff08;提高效率&#xff09; 1…

编程笔记 Golang基础 027 结构体

编程笔记 Golang基础 027 结构体 一、结构体的定义二、结构体的实例化1. 直接初始化2. 使用键值对初始化&#xff08;即使字段顺序不一致也能正确赋值&#xff09;3. 部分初始化&#xff08;未指定的字段会得到它们类型的零值&#xff09;4. 使用var声明和初始化5. 结构体字面量…

C语言第三十弹---自定义类型:结构体(上)

✨个人主页&#xff1a; 熬夜学编程的小林 &#x1f497;系列专栏&#xff1a; 【C语言详解】 【数据结构详解】 结构体 1、结构体类型的声明 1.1、结构体回顾 1.1.1、结构的声明 1.1.2、结构体变量的创建和初始化 1.2、结构的特殊声明 1.3、结构的自引用 2、结构体内存…

K8S—集群调度

目录 前言 一 List-Watch 1.1 list-watch概述 1.2 list-watch工作机制 二 集群调度 2.1 调度过程 2.2 Predicate 和 Priorities 的常见算法和优先级选项 2.3 调度方式 三 亲和性 3.1 节点亲和性 3.2 Pod 亲和性 3.3 键值运算关系 3.4 Pod亲和性与反亲和性 3.5 示例…

音视频数字化(数字与模拟-电影)

针对电视屏幕,电影被称为“大荧幕”,也是娱乐行业的顶尖产业。作为一项综合艺术,从被发明至今,近200年的发展史中,无人可以替代,并始终走在时代的前列。 电影回放的原理就是“视觉残留”,也就是快速移过眼前的画面,会在人的大脑中残留短暂的时间,随着画面不断地移过,…

【C++】C语言可变函数参数 | C++11可变参数模板

文章目录 C语言的可变函数参数遍历va_list逐个取出参数 C可变模板参数递归展开模板参数包示例代码逗号表达式展开参数包 总结 C11的新特性可变参数模板能够让您创建可以接受可变参数的函数模板和类模板&#xff0c;相比C98/03&#xff0c;类模版和函数模版中只能含固定数量的模…

暑期宅家?计算机专业必看的8部电影!一定要安利给你们!

代码编程看上去枯燥乏味&#xff0c;但也是艺术的&#xff0c;感性的&#xff0c;计算机编程的许多概念被应用于电影中&#xff0c;其中有些非常之酷炫&#xff0c;它们甚至能帮助开发人员理解一些编程概念。 所以今天学姐来给大家推荐几部心中top级的编程人必看电影&#xff0…

[C++]C++ 中的 #if 、#ifndef 和 #ifdef

#ifndef 和 #ifdef 是一种宏定义判断&#xff0c;作用是防止多重定义。#ifndef 是 if not define 的简写&#xff0c;#ifdef 是 if define 的简写。 使用格式如下&#xff1a; #if (判断条件) 程序段1 #else 程序段2 #endif#ifdef 标识符 程序段1 #else 程序段2 #endif#ifnde…

nginx(二)

nginx的验证模块 输入用户名和密码 第一步先下载httpd 这个安装包 第二步编辑子配置文件 然后去网页访问192.168.68.3/admin/ 连接之后&#xff0c;会出现404&#xff0c;404出现是因为没给网页写页面 如果要写页面&#xff0c;则在/opt/html&#xff0c;建立一个admin&#x…

max_element和min_element使用

头文件 #include<alorithm> 作用 用于返回数组或容器中最值元素(最小值、最大值)&#xff0c;值和下标。 使用举例 #include<iostream> #include<vector> #include<algorithm> using namespace std; int main() {/*数组初始化*/vector<int>…

软件设计师软考题目解析05 --每日五题

想说的话&#xff1a;要准备软考了。0.0&#xff0c;其实我是不想考的&#xff0c;但是吧&#xff0c;由于本人已经学完所有知识了&#xff0c;只是被学校的课程给锁在那里了&#xff0c;不然早找工作去了。寻思着反正也无聊&#xff0c;就考个证玩玩。 本人github地址&#xf…

微信小程序返回上一级页面并自动刷新数据

文章目录 前言一、获取小程序栈二、生命周期触发总结 前言 界面由A到B&#xff0c;在由B返回A&#xff0c;触发刷新动作 一、获取小程序栈 界面A代码 shuaxin(){//此处可进行接口请求从而实现更新数据的效果console.log("刷新本页面数据啦")},界面B代码 // 返回触…

spring缓存的使用

Spring缓存使用 缓存注解 对于Spring&#xff0c;缓存组件例如EhCache是可拔插的&#xff0c;而缓存注解是通用的。 Cacheable 标记在方法或者类上&#xff0c;标识该方法或类支持缓存。Spring调用注解标识方法后会将返回值缓存到redis&#xff0c;以保证下次同条件调用该方…

C++mciSendString开发软件:音乐播放器2.0

目录 目录 目录 回顾上集代码 开始更改! 1、增加色彩特效 效果 2、增加命令行 初始化界面 获取时长 结构体 设置时长 退出 切换音乐 ......(没用的一堆玩意儿) 完整代码 回顾上集代码 #include <Button.h> #include <heker.h> #include <ARTTEXT.h> …

StopWatch的使用

StopWatch的使用 【一】简介【1】不使用StopWatch的案例【2】使用StopWatch的案例 【二】源码分析【1】查看源码【2】StopWatch优缺点&#xff1a; 【一】简介 stopWatch是org.springframework.util包下的一个工具类&#xff0c;使用它可直观的输出代码执行耗时&#xff0c;以…

MySQL--索引结构

索引-索引结构 1. 概述2. 二叉树3. B-Tree4. BTree5. Hash 1. 概述 MySQL的索引是在存储引擎层实现的&#xff0c;不同的存储引擎有不同的索引结构&#xff0c;主要包含以下几种&#xff1a; 上述是MySQL中所支持的所有的索引结构&#xff0c;下面展示不同的存储引擎对于索引…

JDK下载安装

资源展示 安装说明 傻瓜式安装&#xff0c;下一步即可。建议&#xff1a;安装路径不要有中文或者空格等特殊符号。本套课程会同时安装JDK8 和 JDK17&#xff0c;并以JDK17为默认版本进行讲解。 安装步骤 &#xff08;1&#xff09;双击jdk-17_windows-x64_bin.exe文件&#…

【Eureka详细讲解】

Eureka介绍和使用 1. Eureka 介绍2. Eureka 的主要特点3. 使用3.1 设置 Eureka Server3.2 设置 Eureka Client3.3 Eureka Server 高可用配置 1. Eureka 介绍 Eureka 是由 Netflix 开源的一种服务发现解决方案&#xff0c;它是 Netflix OSS 套件中的一个组件&#xff0c;经常用…