【回眸】Linux 内核 (十五) 之 多线程编程 上

前言

进程和线程 区别

线程API

1.创建线程

2.线程退出 

3.线程等待

4.线程脱离

5. 线程ID获取及比较 

6.创建及销毁互斥锁

7.创建及销毁条件变量

 8. 等待

9.触发

多线程编程 

后记

前言

高产的几天。

进程和线程 区别

进程——资源分配的最小单位,线程——程序执行的最小单位

进程有独立的地址空间,线程没有单独的地址空间(同一进程内的线程共享进程的地址空间)。

线程API

多线程开发在 Linux 平台上已经有成熟的 pthread 库支持。其涉及的多线程开发的最基本概念主要包含三点:线程,互斥锁,条件。其中,线程操作又分线程的创建,退出,等待 3 种。互斥锁则包括 4 种操作,分别是创建,销毁,加锁和解锁。条件操作有 5 种操作:创建,销毁,触发,广播和等待。

1.创建线程

#include <pthread.h>
int pthread_create(pthread_t *restrict tidp, const pthread_attr_t *restrict attr, void *(*start_rtn)(void *), void *restrict arg);
// 返回:若成功返回0,否则返回错误编号

2.线程退出 

#include <pthread.h>
int pthread_exit(void *rval_ptr);

3.线程等待

#include <pthread.h>
int pthread_join(pthread_t thread, void **rval_ptr);
// 返回:若成功返回0,否则返回错误编号

4.线程脱离

#include <pthread.h>
int pthread_detach(pthread_t thread);
// 返回:若成功返回0,否则返回错误编号

5. 线程ID获取及比较 

#include <pthread.h>
pthread_t pthread_self(void);
// 返回:调用线程的ID
#include <pthread.h>
int pthread_equal(pthread_t tid1, pthread_t tid2);
// 返回:若相等则返回非0值,否则返回0

6.创建及销毁互斥锁

#include <pthread.h>
int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr);
int pthread_mutex_destroy(pthread_mutex_t mutex);
// 返回:若成功返回0,否则返回错误编号
#include <pthread.h>
int pthread_mutex_lock(pthread_mutex_t *mutex)
int pthread_mutex_trylock(pthread_mutex_t *mutex);
int pthread_mutex_unlock(pthread_mutex_t *mutex);
// 返回:若成功返回0,否则返回错误编号

7.创建及销毁条件变量

#include <pthread.h>
int pthread_cond_init(pthread_cond_t *restrict cond, const pthread_condattr_t *restrict attr);
int pthread_cond_destroy(pthread_cond_t cond);
// 返回:若成功返回0,否则返回错误编号

 8. 等待

#include <pthread.h>
int pthread_cond_wait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex);
int pthread_cond_timedwait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex, cond struct timespec *restrict timeout);
// 返回:若成功返回0,否则返回错误编号

9.触发

#include <pthread.h>
int pthread_cond_signal(pthread_cond_t cond);
int pthread_cond_broadcast(pthread_cond_t cond);
// 返回:若成功返回0,否则返回错误编号

多线程编程 

thread1.c(线程创建)

#include <stdio.h>
#include <pthread.h>void *func1(void *arg){printf("t1:%ld thread is created.\n",(unsigned long)pthread_self());
printf("t1:param is %d\n",*((int *)arg));}int main(){
int ret;
int param =100;
pthread_t t1;
ret = pthread_create(&t1,NULL,func1,(void *)&param);
if(ret == 0){
printf("main:Create t1 sucess.\n");
}
printf("mainID:%ld .\n",(unsigned long)pthread_self());
while(1);return 0;
}

thread2.c(等待和退出,传整数)

#include <stdio.h>
#include <pthread.h>void *func1(void *arg){
static int ret = 10;
printf("t1:%ld thread is created.\n",(unsigned long)pthread_self());
printf("t1:param is %d\n",*((int *)arg));
pthread_exit((void *)&ret);}int main(){
int ret;
int param =100;
int *pret;
pthread_t t1;
ret = pthread_create(&t1,NULL,func1,(void *)&param);
if(ret == 0){
printf("main:Create t1 sucess.\n");
}
printf("mainID:%ld .\n",(unsigned long)pthread_self());
//while(1);
pthread_join(t1,(void **)&pret);
printf("main:t1 exit. ret = %d\n", *pret);
return 0;
}

thread3.c(等待和退出 传字符串) 

#include <stdio.h>
#include <pthread.h>void *func1(void *arg){
static char *p = "t1 pijiuya1 is runing.\n";
printf("t1:%ld thread is created.\n",(unsigned long)pthread_self());
printf("t1:param is %d\n",*((int *)arg));
pthread_exit((void *)p);//退出}int main(){
int ret;
int param =100;
char *pret = NULL;
pthread_t t1;
ret = pthread_create(&t1,NULL,func1,(void *)&param);
if(ret == 0){
printf("main:Create t1 sucess.\n");
}
printf("mainID:%ld .\n",(unsigned long)pthread_self());
//while(1);
pthread_join(t1,(void **)&pret);//等待
printf("main:t1 exit. ret = %s\n", pret);
return 0;
}

thread4.c(创建2个)

#include <stdio.h>
#include <pthread.h>void *func1(void *arg){
static int ret = 10;
printf("t1:%ld thread is created.\n",(unsigned long)pthread_self());
printf("t1:param is %d\n",*((int *)arg));
pthread_exit((void *)&ret);}int main(){
int ret;
int param =100;
int *pret;
pthread_t t1;
ret = pthread_create(&t1,NULL,func1,(void *)&param);
if(ret == 0){
printf("main:Create t1 sucess.\n");
}
printf("mainID:%ld .\n",(unsigned long)pthread_self());
//while(1);
pthread_join(t1,(void **)&pret);
printf("main:t1 exit. ret = %d\n", *pret);
return 0;
}

 thread5.c(创建2个且打印顺序发现多线程共享同一段内存【顺序没有重复】)

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>int g_data = 0;
void *func1(void *arg){printf("t1:%ld thread is created.\n",(unsigned long)pthread_self());
printf("t1:param is %d\n",*((int *)arg));
while(1){
printf("t1:%d\n",g_data++);
sleep(1);
};}
void *func2(void *arg){
printf("t2:%ld thread is created.\n",(unsigned long)pthread_self());
printf("t2:param is %d\n",*((int *)arg));
while(1){
printf("t2:%d\n",g_data++);
sleep(1);
};}
int main(){
int ret;
int param =100;
pthread_t t1;
pthread_t t2;
ret = pthread_create(&t1,NULL,func1,(void *)&param);
if(ret == 0){
printf("main:Create t1 sucess.\n");
}
printf("mainID:%ld .\n",(unsigned long)pthread_self());
ret = pthread_create(&t2,NULL,func2,(void *)&param);
if(ret == 0){
printf("main:Create t2 sucess.\n");
}
printf("mainID:%ld .\n",(unsigned long)pthread_self());
while(1){
printf("main:%d\n",g_data++);
sleep(1);
};
pthread_join(t1,NULL);//等待
pthread_join(t2,NULL);//等待
return 0;
}

 

抢占资源是随机的,每次运行的结果不尽相同。

thread6.c(创建3个线程并且加锁,让t1优先运行且不被t2 t3 打断,t1 释放锁后,t3和t2竞争资源)

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>pthread_mutex_t mutex;
pthread_attr_t attr;
void *func1(void *arg){
int i ;
pthread_mutex_lock(&mutex);//保证t1优先
for(i = 0;i<5;i++){
printf("t1:%ld thread is created.\n",(unsigned long)pthread_self());
printf("t1:param is %d\n",*((int *)arg));
}
pthread_mutex_unlock(&mutex);}
void *func2(void *arg){
pthread_mutex_lock(&mutex);
printf("t2:%ld thread is created.\n",(unsigned long)pthread_self());
printf("t2:param is %d\n",*((int *)arg));
pthread_mutex_unlock(&mutex);
}
void *func3(void *arg){
pthread_mutex_lock(&mutex);
printf("t3:%ld thread is created.\n",(unsigned long)pthread_self());
printf("t3:param is %d\n",*((int *)arg));
pthread_mutex_unlock(&mutex);
}
int main(){
int ret;
int param =100;
pthread_t t1;
pthread_t t2;
pthread_t t3;
pthread_mutex_init(&mutex,NULL);
ret = pthread_create(&t1,NULL,func1,(void *)&param);
if(ret == 0){
printf("main:Create t1 sucess.\n");
}
ret = pthread_create(&t2,NULL,func2,(void *)&param);
if(ret == 0){
printf("main:Create t2 sucess.\n");
}
ret = pthread_create(&t3,NULL,func3,(void *)&param);
if(ret == 0){
printf("main:Create t3 sucess.\n");
}
printf("mainID:%ld .\n",(unsigned long)pthread_self());pthread_join(t1,NULL);//等待
pthread_join(t2,NULL);//等待
pthread_join(t3,NULL);//等待
pthread_attr_destroy(&attr);
return 0;
}

 

 thread7.c(fun1先运行到10退出锁,func2后运行不退出)

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>pthread_mutex_t mutex;
pthread_attr_t attr;
int g_data = 0;
void *func1(void *arg){
pthread_mutex_lock(&mutex);//保证t1优先
while(1){
printf("t1:%d\n",g_data++);
sleep(1);
if (g_data == 10){
pthread_mutex_unlock(&mutex);
pthread_exit(NULL);
}
}}
void *func2(void *arg){printf("t2:%ld thread is created.\n",(unsigned long)pthread_self());
printf("t2:param is %d\n",*((int *)arg));
while(1){
pthread_mutex_lock(&mutex);
printf("t2:%d\n",g_data);
g_data++;
pthread_mutex_unlock(&mutex);
sleep(1); 
}
}
int main(){
int ret;
int param =100;
pthread_t t1;
pthread_t t2;
pthread_mutex_init(&mutex,NULL);
ret = pthread_create(&t1,NULL,func1,(void *)&param);
if(ret == 0){
printf("main:Create t1 sucess.\n");
}
ret = pthread_create(&t2,NULL,func2,(void *)&param);
if(ret == 0){
printf("main:Create t2 sucess.\n");
}
printf("mainID:%ld .\n",(unsigned long)pthread_self());
printf("main:g_data = %d.\n",g_data);
pthread_join(t1,NULL);//等待
pthread_join(t2,NULL);//等待
pthread_attr_destroy(&attr);
return 0;
}

后记

内容实在是有点多,故而分两篇博文。这个多线程的内容在Java里代码很少,回顾了linux这里的互斥锁,感受更加不一样,比当时更加理解了互斥锁的问题。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/bicheng/76251.shtml

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

相关文章

127.0.0.1本地环回地址(Loopback Address)

127.0.0.1 是计算机网络中的一个特殊IPv4地址&#xff0c;称为本地环回地址&#xff08;Loopback Address&#xff09;&#xff0c;主要用于以下用途&#xff1a; 1. 基本定义 本地主机&#xff08;Localhost&#xff09;&#xff1a;该地址始终指向当前正在使用的计算机本身&a…

S7-1200 PLC热电偶和热电阻模拟量模块

热电偶和热电阻模拟量模块 S7-1200 PLC有专用用于对温度进行采集的热电偶模块SM1231 TC和SM 1231RTD。热电偶模块有4AI和8AI两种&#xff0c;下面以SM1231 TC 4AI为例看一下接线图。 该模块一共有4个通道&#xff0c;每个通道有两个接线端子&#xff0c;比如0&#xff0c;0-。…

深度了解向量引论

今天去研究了一个基本数学原理 这个其实需要证明 今天推导了一下这个公式&#xff0c;感觉收获挺大 下面是手工推导过程

Feign修仙指南:声明式HTTP请求的优雅之道

各位在微服务世界摸爬滚打的道友们&#xff01;今天要解锁的是Spring Cloud的绝世神通——Feign&#xff01;这货堪称HTTP界的"言出法随"&#xff0c;只需定义接口&#xff0c;就能自动生成HTTP请求代码&#xff01;从此告别手动拼装URL的苦日子&#xff0c;让你的代…

UDP学习笔记(四)UDP 为什么大小不能超过 64KB?

&#x1f310; UDP 为什么大小不能超过 64KB&#xff1f;TCP 有这个限制吗&#xff1f; 在进行网络编程或者调试网络协议时&#xff0c;我们常常会看到一个说法&#xff1a; “UDP 最大只能发送 64KB 数据。” 这到底是怎么回事&#xff1f;这 64KB 是怎么来的&#xff1f;TCP…

LabVIEW 中串口设备与采集卡的同步精度

在 LabVIEW 项目开发中&#xff0c;常涉及多种设备协同工作&#xff0c;如通过串口设备采集温度&#xff0c;利用采集卡&#xff08;如 NI 6251&#xff09;采集压力。此时&#xff0c;设备间的同步精度至关重要&#xff0c;它直接影响系统数据的准确性与可靠性。下面&#xff…

DP_AUX辅助通道介绍

DisplayPort&#xff08;简称DP&#xff09;是一个由PC及芯片制造商联盟开发&#xff0c;视频电子标准协会&#xff08;VESA&#xff09;标准化的数字式视频接口标准。该接口免认证、免授权金&#xff0c;主要用于视频源与显示器等设备的连接&#xff0c;并也支持携带音频、USB…

[GESP202312 五级] 平均分配

文章目录 题目描述输入格式输出格式输入输出样例 #1输入 #1输出 #1 输入输出样例 #2输入 #2输出 #2 提交链接提示解析参考代码 题目描述 小杨认为&#xff0c;所有大于等于 a a a 的完全平方数都是他的超级幸运数。 小杨还认为&#xff0c;所有超级幸运数的倍数都是他的幸运…

[Mysql]buffersize修改

1、找到my.cnf文件位置 ps -ef|grep mysqld 2、编辑my.cnf cd /etc/my.cnf.d vim my.cnf 一般修改为内存的50%~70% 3、重启服务 systemctl restart mysqld

清晰易懂的 Apollo 配置中心安装与使用教程

Apollo 是携程开源的分布式配置管理平台&#xff0c;支持配置实时推送、版本管理、权限控制等功能。本教程将手把手教你完成 Apollo 核心组件安装、基础配置管理及避坑指南&#xff0c;助你快速掌握企业级配置管理能力。 一、环境准备&#xff08;关键依赖&#xff09; 1. 基础…

PyTorch池化层详解:原理、实现与示例

池化层&#xff08;Pooling Layer&#xff09;是卷积神经网络中的重要组成部分&#xff0c;主要用于降低特征图的空间维度、减少计算量并增强模型的平移不变性。本文将通过PyTorch代码演示池化层的实现原理&#xff0c;并详细讲解最大池化、平均池化、填充&#xff08;Padding&…

如何构建并优化提示词?

提示词是一个小白最容易上手大模型的方式&#xff0c;提示词就是你告诉大模型应该如何去完成一项工作的系统性的命令&#xff0c;所以写一个好的提示词是比较关键的&#xff0c;那么如何写好一个提示词呢&#xff1f; 要写好提示词&#xff0c;其实就像我们要把一些命令清晰地传…

面向大模型的开发框架LangChain

这篇文章会带给你 如何使用 LangChain&#xff1a;一套在大模型能力上封装的工具框架如何用几行代码实现一个复杂的 AI 应用面向大模型的流程开发的过程抽象 文章目录 这篇文章会带给你写在前面LangChain 的核心组件文档&#xff08;以 Python 版为例&#xff09;模型 I/O 封装…

【蓝桥杯】动态规划:线性动态规划

1. 最长上升子序列(LIS) 1.1. 题目 想象你有一排数字,比如:3, 1, 2, 1, 8, 5, 6 你要从中挑出一些数字,这些数字要满足两个条件: 你挑的数字的顺序要和原来序列中的顺序一致(不能打乱顺序) 你挑的数字要一个比一个大(严格递增) 问:最多能挑出多少个这样的数字? …

vue2和vue3的主要区别

一、性能优化与响应式系统 性能优化&#xff1a; Vue2&#xff1a;性能较好&#xff0c;但在大型应用中&#xff0c;当数据变化频繁时可能出现性能瓶颈。它使用虚拟DOM来高效地进行DOM操作&#xff0c;并通过多种技术手段如懒加载、异步组件、树形抖动等优化性能。 Vue3&…

Python: 实现数据可视化分析系统

后端基于Python 开源的 Web 框架 Flask&#xff0c;前端页面采用 LayUI 框架以及 Echarts 图表&#xff0c;数据库为sqlite。系统的功能模块分为数据采集和存储模块、数据处理和分析模块、可视化展示模块和系统管理模块。情感分析方面使用LDA等主题建模技术&#xff0c;结合领域…

深度学习总结(3)

数据批量的概念 通常来说&#xff0c;深度学习中所有数据张量的第一个轴&#xff08;也就是轴0&#xff0c;因为索引从0开始&#xff09;都是样本轴[samples axis&#xff0c;有时也叫样本维度&#xff08;samples dimension&#xff09;​]​。深度学习模型不会一次性处理整个…

微软庆祝它成立整整50周年

每周跟踪AI热点新闻动向和震撼发展 想要探索生成式人工智能的前沿进展吗&#xff1f;订阅我们的简报&#xff0c;深入解析最新的技术突破、实际应用案例和未来的趋势。与全球数同行一同&#xff0c;从行业内部的深度分析和实用指南中受益。不要错过这个机会&#xff0c;成为AI领…

【操作系统(Linux)】——通过案例学习父子进程的线程异步性

本篇旨在通过几个案例来学习父子进程的线程异步性 一、父进程与子进程 我们将要做的&#xff1a; 创建父子进程&#xff0c;观察父子进程执行的顺序&#xff0c;了解进程执行的异步行为 源代码&#xff1a; #include <stdio.h> #include <sys/types.h> #include…

系统性能核心指标:QPS、TPS、RT、并发量详解

系统性能核心指标&#xff1a;QPS、TPS、RT、并发量详解 1. 引言 在分布式系统、高并发架构设计中&#xff0c;QPS、TPS、RT、并发量 等指标是衡量系统性能的关键。本文深入解析这些术语的定义、计算方法、关联性及优化策略&#xff0c;帮助开发者更好地进行系统性能评估与调…