15-LINUX--线程的创建与同步
一.线程
1.线程的概念
线程是进程内部的一条执行序列或执行路径,一个进程可以包含多条线程。
2.线程的三种实现方式
线程同步的方法:信号量,互斥锁,条件变量,读写锁
Ps -elf 查看线程ID
Linux 中线程的实现:
3.进程与线程的区别
二.线程使用
1.线程库
#include <pthread.h>/*pthread_create()用于创建线程thread: 接收创建的线程的 IDattr: 指定线程的属性start_routine: 指定线程函数arg: 给线程函数传递的参数成功返回 0, 失败返回错误码*/int pthread_create(pthread_t *thread, const pthread_attr_t *attr,void *(*start_routine) (void *), void *arg);13./*pthread_exit()退出线程retval:指定退出信息*/int pthread_exit(void *retval);/*pthread_join()等待 thread 指定的线程退出,线程未退出时,该方法阻塞retval:接收 thread 线程退出时,指定的退出信息
*/int pthread_join(pthread_t thread, void **retval);
多线程代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <unistd.h>
#include <pthread.h>void * pthread_fun(void *arg){int i = 0;for(; i < 5; ++i){sleep(1);printf("fun thread running\n");}pthread_exit("fun over");}int main(){pthread_t tid;int res = pthread_create(&tid, NULL, pthread_fun, NULL);assert(res == 0);int i = 0;for(; i < 5; ++i){sleep(1);printf("main thread running\n");}char *s = NULL;pthread_join(tid, (void **)&s);printf("s = %s\n", s);exit(0);}
三.线程同步
1.互斥锁
#include <pthread.h>int pthread_mutex_init(pthread_mutex_t *mutex, pthread_mutexattr_t *attr);//初始化锁int pthread_mutex_lock(pthread_mutex_t *mutex);//上锁,其他线程无法使用int pthread_mutex_unlock(pthread_mutex_t *mutex);//开锁int pthread_mutex_destroy(pthread_mutex_t *mutex);//销毁锁
因为线程引入共享了进程的地址空间,导致了一个线程操作数据时候,极其容易影响到其他线程的情况;对其他线程造成不可控因素,或引起异常,逻辑结果不正确的情况;这也是线程不安全的原因!

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<pthread.h>
#include<semaphore.h>//sem_t sem;
pthread_mutex_t mutex;
void* fun1(void* arg)
{for(int i=0;i<5;i++){//sem_wait(&sem);pthread_mutex_lock(&mutex);printf("A");fflush(stdout);int n=rand()%3;sleep(n);printf("A");fflush(stdout);//sem_post(&sem);pthread_mutex_unlock(&mutex);n=rand()%3;sleep(n);}
}
void* fun2(void* arg)
{for(int i=0;i<5;i++){//sem_wait(&sem);pthread_mutex_lock(&mutex);printf("B");fflush(stdout);int n=rand()%3;sleep(n);printf("B");fflush(stdout);//sem_post(&sem);pthread_mutex_unlock(&mutex);n=rand()%3;sleep(n);}}
int main()
{//sem_init(&sem,0,1);pthread_mutex_init(&mutex,NULL);pthread_t id1,id2;pthread_create(&id1,NULL,fun1,NULL);pthread_create(&id2,NULL,fun2,NULL);pthread_join(id1,NULL);pthread_join(id2,NULL);//sem_destroy(&sem);pthread_mutex_destroy(&mutex);exit(0);
}
2.信号量
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>
#include <fcntl.h>char buff[128] = {0};sem_t sem1;sem_t sem2;void* PthreadFun(void *arg)
{int fd = open("a.txt", O_RDWR | O_CREAT, 0664);assert(fd != -1);//函数线程完成将用户输入的数据存储到文件中while(1){sem_wait(&sem2);if(strncmp(buff, "end", 3) == 0){break;}write(fd, buff, strlen(buff));memset(buff, 0, 128);sem_post(&sem1);}sem_destroy(&sem1);sem_destroy(&sem2);}int main(){sem_init(&sem1, 0, 1);sem_init(&sem2, 0, 0);pthread_t id;int res = pthread_create(&id, NULL, PthreadFun, NULL);assert(res == 0);//主线程完成获取用户数据的数据,并存储在全局数组 buff 中while(1){sem_wait(&sem1);printf("please input data: ");fflush(stdout);fgets(buff, 128, stdin);buff[strlen(buff) - 1] = 0;sem_post(&sem2);if(strncmp(buff, "end", 3) == 0){break;}}pthread_exit(NULL);}
3.条件变量
条件变量是利用线程间共享的全局变量进行同步的一种机制。
主要包括两个动作:一个线程等待”条件变量的条件成立”而挂起;另一个线程使”条件成立”(给出条件成立信号)。
为了防止竞争,条件变量的使用总是和一个互斥锁结合在一起。
条件变量类型为 pthread_cond_t。
条件变量接口
pthread_cond_init() //初始化
pthread_cond_wait() //等待将信息存入并等待达到唤醒条件
pthread_cond_signal() //只唤醒一个
pthread_cond_broadcast() //唤醒所以有的线程
条件变量有什么用
使用条件变量可以以原子方式阻塞线程,直到某个特定条件为真为止。条件变量始终与互斥锁一起使用,对条件的测试是在互斥锁(互斥)的保护下进行的。
如果条件为假,线程通常会基于条件变量阻塞,并以原子方式释放等待条件变化的互斥锁。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>pthread_cond_t cond;
pthread_mutex_t mutex;
void* funa(void* arg)
{char* s =(char*)arg;while(1){pthread_mutex_lock(&mutex);pthread_cond_wait(&cond,&mutex);//添加到条件变量的等待队列,阻塞pthread_mutex_unlock(&mutex);if(strncmp(s,"end",3)==0){break;}printf("funa:%s\n",s);}
}
void* funb(void* arg)
{char* s =(char*)arg;while(1){pthread_mutex_lock(&mutex);pthread_cond_wait(&cond,&mutex);//添加到条件变量的等待队列,阻塞pthread_mutex_unlock(&mutex);if(strncmp(s,"end",3)==0){break;}printf("funb:%s\n",s);}}
int main()
{pthread_mutex_init(&mutex,NULL);pthread_cond_init(&cond,NULL);pthread_t id1,id2;char buff[128]={0};pthread_create(&id1,NULL,funa,buff);pthread_create(&id2,NULL,funb,buff);while(1){char tmp[128]={0};fgets(tmp,128,stdin);strcpy(buff,tmp);if(strncmp(tmp,"end",3)==0){pthread_mutex_lock(&mutex);pthread_cond_broadcast(&cond);//唤醒所有线程pthread_mutex_unlock(&mutex);break;}else{pthread_mutex_lock(&mutex);pthread_cond_signal(&cond);//唤醒一个pthread_mutex_unlock(&mutex);}}pthread_join(id1,NULL);pthread_join(id2,NULL);pthread_mutex_destroy(&mutex);pthread_cond_destroy(&cond);exit(0);
}
4.读写锁
读写锁:是一对锁,分为读锁和写锁,允许多个线程同时获取读写锁,但再通过一时间,只允许一个线程获得写锁,或者可以由多个线程获得读锁
接口:
#include <pthread.h>int pthread_rwlock_init(pthread_rwlock_t *rwlock, pthread_rwlockattr_t *attr);int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);int pthread_rwlock_destroy(pthread_rwlock_t *rwlock);
代码示例
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<pthread.h>pthread_rwlock_t lock;void* fun_r1(void* arg)
{for(int i=0;i<10;i++){pthread_rwlock_rdlock(&lock);printf("fun r1 start\n");int n=rand()%3;sleep(n);printf("fun r1 end\n");pthread_rwlock_unlock(&lock);n=rand()%3;sleep(n);}
}void* fun_r2(void* arg)
{for(int i=0;i<10;i++){pthread_rwlock_rdlock(&lock);printf("fun r2 start\n");int n=rand()%3;sleep(n);printf("fun r2 end\n");pthread_rwlock_unlock(&lock);n=rand()%3;sleep(n);}}void* fun_w(void* arg)
{for(int i=0;i<10;i++){pthread_rwlock_wrlock(&lock);printf(" fun w start\n");int n=rand()%3;sleep(n);printf(" fun w end\n");pthread_rwlock_wrlock(&lock);n = rand()%3;sleep(n);}
}int main()
{pthread_rwlock_init(&lock,NULL);pthread_t id1,id2,id3;pthread_create(&id1,NULL,fun_r1,NULL);pthread_create(&id2,NULL,fun_r2,NULL);pthread_create(&id3,NULL,fun_w,NULL);pthread_join(id1,NULL);pthread_join(id2,NULL);pthread_join(id3,NULL);pthread_rwlock_destroy(&lock);exit(0);
}
相关文章:

15-LINUX--线程的创建与同步
一.线程 1.线程的概念 线程是进程内部的一条执行序列或执行路径,一个进程可以包含多条线程。 2.线程的三种实现方式 ◼ 内核级线程:由内核创建,创建开销大,内核能感知到线程的存在 ◼ 用户级线程:线程的创建有用户空…...
【退役之重学Java】如何解决消息持续积压等问题
一、将读写数据库等耗时的操作,从消费者逻辑中抽取出来,专门部署机器去完成这部分操作。...

Linux下的SPI通信
SPI通信 一. 1.SPI简介: SPI 是一种高速,全双工,同步串行总线。 SPI 有主从俩种模式通常由一个主设备和一个或者多个从设备组从。SPI不支持多主机。 SPI通信至少需要四根线,分别是 MISO(主设备数据输入,从设备输出),MOSI (主设数据输出从设备输入),SCLK(时钟信号),CS/SS…...

【转载】数字化工厂规划蓝图报告
制造业进入到全新的数字化时代,需要构建新型智能工厂、数字化工厂与智能车间以助力传统产业智能制造升级,将新一代信息技术贯穿到设计、工艺、生产、物流等各个环节。目的是完善创新体系、提升产品质量、推行绿色制造、增强核心竞争力、发展现代化客户体…...

《基于GNU-Radio和USRP的雷达通信系统的实现》文献阅读
文章目录 前言一、摘要二、引言三、联合系统实施1、基本原理2、实验方案 四、软件设置1、发射机2、接收机 五、实验结果1、实验设置2、波形3、室内外对比4、不同参数的结果 六、结论七、参考文献八、论文自取九、阅读收获 前言 本文记录《基于GNU-Radio和USRP的雷达通信系统的实…...

Sealos急速部署生产用k8s集群
最近一段时间部署k8s全部使用sealos了,整体使用感觉良好,基本没有什么坑。推荐给大家。 使用 Sealos,可以安装一个不包含任何组件的裸 Kubernetes 集群。 最大的好处是提供 99 年证书,用到我跑路是足够了。不用像之前kubeadm安装…...

VTK数据的读写--Vtk学习记录1--《VTK图形图像开发进阶》
读和写操作是VTK可视化管线两端相关的类--Reader和Writer类 Reader:将外部数据读入可视化管线,主要步骤如下 s1:实例化Reader对象 s2:指定所要读取的文件名 s3:调用Update()促使管线执行 对应的Writer: s1:实例化Writer对象 s2输入要写的数据以及指定写入的文…...

Vue3专栏项目 -- 一、第一个页面(下)
一、Dropdown 组件(下拉菜单组件)编码 1、基本功能:展示出下拉按钮和下拉菜单栏的样式 我们可以通过bootstrap来实现这个下拉框,需要注意它这个只是有样式,是没有行为的 然后这个下拉按钮的文字展示是根据用户名称展…...
一栈走天下:使用HBuilderX高效搭建Uni-App微信小程序开发环境
一栈走天下:使用HBuilderX高效搭建Uni-App微信小程序开发环境 Uni-App与HBuilderX简介Uni-App基础HBuilderX介绍 环境搭建步骤步骤1:安装HBuilderX步骤2:创建Uni-App项目步骤3:配置微信小程序平台步骤4:预览与发布 代码…...

docker安装Debian:11 freeswitch1.10.5
文章目录 一、生成一个镜像二、切换一个镜像源为阿里源三、安装一些相关依赖和freeswitch3.1第一步:安装freeswitch-mod和下载所需的依赖项3.2 设置密钥3.3 安装freeswitch所需的依赖项3.4 报错3.4.1 报错13.4.2 报错23.4.3 报错3 四、运行4.1 通话三十秒自动挂断 一…...

c3 笔记6 认识css样式表
<link>与import应该如何选择?事实上,使用link与import链接外部样式文件的效果看起来是一样的,区别在于<link>是HTML标记而import属于CSS语法。<link>标记有rel、type与href属性,可以指定CSS样式表的名称,这样就…...

基于springboot+mybatis+vue的项目实战之增删改查CRUD
目录结构 PeotController.java package com.example.controller;import com.example.pojo.Peot; import com.example.pojo.Result; import com.example.service.PeotService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web…...

字节跳动(社招)四面算法原题
TikTok 进展 又是一期定时汇报 TikTok 进展的推文。 上周,美国总统拜登签署了价值 950 亿美元的一揽子对外援助法案。 该法案涉及强制字节跳动剥离旗下应用 TikTok 美国业务,即 针对 TikTok 非卖即禁的"强抢行为"开始进入九个月(27…...
车道线检测交通信号识别车辆实时检测
系列文章目录 提示:这里可以添加系列文章的所有文章的目录,目录需要自己手动添加 TODO:写完再整理 文章目录 系列文章目录前言车道线检测机器学习前言 认知有限,望大家多多包涵,有什么问题也希望能够与大家多交流,共同成长! 本文先对车道线检测&交通信号识别&…...
用正则表达式打造免费代理IP池
爬虫的过程中,当对方服务器发现你屡次爬取它,可能会遇到被封IP的苦痛,这时IP就应该换啦,打造IP池的意义十分重要,提供免费IP网站有很多,本次用的是西刺代理IP # -*- coding: utf-8 -*- """…...

【每日刷题】Day35
【每日刷题】Day35 🥕个人主页:开敲🍉 🔥所属专栏:每日刷题🍍 🌼文章目录🌼 1. 844. 比较含退格的字符串 - 力扣(LeetCode) 2. 2487. 从链表中移除节点 - 力…...

Python数据清洗与可视化实践:国际旅游收入数据分析
文章目录 概要整体流程名词解释NumPyPandasMatplotlibre 技术细节数据清洗可视化 小结 概要 在本篇博客中,我们将通过一个实际的案例,演示如何使用Python进行数据清洗和可视化,以分析国际旅游收入数据。我们将使用Python中的Pandas库来进行数…...

前置知识储备
基本认知 什么是模式 在一定环境中解决一些问题的方案(通俗来说:特定环境中用固定的套路解决问题) 什么是设计模式 设计模式是一套反复被人使用,多数人知晓的,经过分类编目的代码设计经验的总结 设计模式最终的目…...

六月品牌互动营销方案的作用是什么
品牌需要借势营销,六月的六个节日热点,是企业商家不能错过的,如何运用合适的工具/方法借势也同样重要。 互动h5游戏/传单页面发挥不同效果,这份《六月品牌互动营销方案》看看有哪些内容吧~ 1、儿童节 宜:回忆欢乐营销…...
dummy_worker C++ 预占用部分比例cpu资源,人为创造cpu资源紧张
背景 有时候为了C测试程序在cpu资源紧张情况下是否正常,需要人为创造cpu资源紧张 编译方法 g -o dummp_worker dummp_worker.cpp -stdc11 -pthread 使用方法 ./dummp_worker 4 0.2 占用4个cpu核的20%比例的cpu资源 源码 // dummp_worker.cpp #include <c…...

51c自动驾驶~合集58
我自己的原文哦~ https://blog.51cto.com/whaosoft/13967107 #CCA-Attention 全局池化局部保留,CCA-Attention为LLM长文本建模带来突破性进展 琶洲实验室、华南理工大学联合推出关键上下文感知注意力机制(CCA-Attention),…...
镜像里切换为普通用户
如果你登录远程虚拟机默认就是 root 用户,但你不希望用 root 权限运行 ns-3(这是对的,ns3 工具会拒绝 root),你可以按以下方法创建一个 非 root 用户账号 并切换到它运行 ns-3。 一次性解决方案:创建非 roo…...
C++ 基础特性深度解析
目录 引言 一、命名空间(namespace) C 中的命名空间 与 C 语言的对比 二、缺省参数 C 中的缺省参数 与 C 语言的对比 三、引用(reference) C 中的引用 与 C 语言的对比 四、inline(内联函数…...
【android bluetooth 框架分析 04】【bt-framework 层详解 1】【BluetoothProperties介绍】
1. BluetoothProperties介绍 libsysprop/srcs/android/sysprop/BluetoothProperties.sysprop BluetoothProperties.sysprop 是 Android AOSP 中的一种 系统属性定义文件(System Property Definition File),用于声明和管理 Bluetooth 模块相…...
【Web 进阶篇】优雅的接口设计:统一响应、全局异常处理与参数校验
系列回顾: 在上一篇中,我们成功地为应用集成了数据库,并使用 Spring Data JPA 实现了基本的 CRUD API。我们的应用现在能“记忆”数据了!但是,如果你仔细审视那些 API,会发现它们还很“粗糙”:有…...

基于Java+VUE+MariaDB实现(Web)仿小米商城
仿小米商城 环境安装 nodejs maven JDK11 运行 mvn clean install -DskipTestscd adminmvn spring-boot:runcd ../webmvn spring-boot:runcd ../xiaomi-store-admin-vuenpm installnpm run servecd ../xiaomi-store-vuenpm installnpm run serve 注意:运行前…...
tomcat入门
1 tomcat 是什么 apache开发的web服务器可以为java web程序提供运行环境tomcat是一款高效,稳定,易于使用的web服务器tomcathttp服务器Servlet服务器 2 tomcat 目录介绍 -bin #存放tomcat的脚本 -conf #存放tomcat的配置文件 ---catalina.policy #to…...
掌握 HTTP 请求:理解 cURL GET 语法
cURL 是一个强大的命令行工具,用于发送 HTTP 请求和与 Web 服务器交互。在 Web 开发和测试中,cURL 经常用于发送 GET 请求来获取服务器资源。本文将详细介绍 cURL GET 请求的语法和使用方法。 一、cURL 基本概念 cURL 是 "Client URL" 的缩写…...

Cilium动手实验室: 精通之旅---13.Cilium LoadBalancer IPAM and L2 Service Announcement
Cilium动手实验室: 精通之旅---13.Cilium LoadBalancer IPAM and L2 Service Announcement 1. LAB环境2. L2公告策略2.1 部署Death Star2.2 访问服务2.3 部署L2公告策略2.4 服务宣告 3. 可视化 ARP 流量3.1 部署新服务3.2 准备可视化3.3 再次请求 4. 自动IPAM4.1 IPAM Pool4.2 …...

何谓AI编程【02】AI编程官网以优雅草星云智控为例建设实践-完善顶部-建立各项子页-调整排版-优雅草卓伊凡
何谓AI编程【02】AI编程官网以优雅草星云智控为例建设实践-完善顶部-建立各项子页-调整排版-优雅草卓伊凡 背景 我们以建设星云智控官网来做AI编程实践,很多人以为AI已经强大到不需要程序员了,其实不是,AI更加需要程序员,普通人…...