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

线程安全学习

1 什么是线程

线程是cpu调度的最小单位,在Linux 下 实现线程的方式为轻量级进程,复用进程的结构体,使用clone函数创建

2 线程安全

所谓线程安全,更确切的应该描述为内存安全

#include <stdio.h>
#include <pthread.h>
int a=1;
void* myfun(){for(int i=0;i<10000;i++){a++;}
}int main(){pthread_t t1, t2;
pthread_create(&t1, NULL,myfun, (void*)1);
pthread_create(&t2, NULL, myfun, (void*)2);
pthread_join(t1, NULL);
pthread_join(t2, NULL);}

上述代码 按照预期应该是 a++20000次,而执行结果却与预期不符,这就是典型的线程不安全

产生原因也很简单,在现代操作系统下 cpu并不是直接访问内存,每个cpu核心都有一个 独立的L0 L1 缓存,以及共享的L2 缓存,在线程1 将 a 从主存写入缓存时,进行++操作,此时a +1 并未回写至主存, 此时线程2 去主存中读取的值仍然为1, 并且 ++操作也非原子操作,多种可能都会导致结果不符合预期

如何实现线程安全

实现的方式有很多种

最简单常见的为使用二进制信号量或者mutex,例如

#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>//sem_t semaphore;//定义信号量
pthread_mutex_t mutex;
int a=1;
void* myfun(){for(int i=0;i<10000;i++){
pthread_mutex_lock(&mutex);
//sem_wait(&semaphore);a++;
pthread_mutex_unlock(&mutex);
//sem_post(&semaphore);
return NULL;}
}int main(){//sem_init(&semaphore, 0, 1);
pthread_t t1, t2;
pthread_create(&t1, NULL,myfun, (void*)1);
pthread_create(&t2, NULL, myfun, (void*)2);
pthread_join(t1, NULL);
pthread_join(t2, NULL);//sem_destroy(&semaphore);
pthread_mutex_destroy(&mutex)}

semaphore 与 mutex 在底层由硬件cpu的原子指令以及缓存一致性协议与内存屏障保证其原子性

二者简单差异 为 semaphore 竞争失败时线程处于自旋状态,而mutex则让出cpu,增加了更多的上线文切换

在java 中常见的 synchronized 以及LOCK 都是使用mutex实现,例如

public class createFun {public static final Integer integer =1;public static int a=0;public static void main(String[] args) throws Exception{Thread t1= new Thread(new Runnable() {@Overridepublic void run() {add();}});Thread t2= new Thread(new Runnable() {@Overridepublic void run() {add();}});t1.start();t2.start();t1.join();t2.join();System.out.println("a="+a);}public  static void add(){for(int i=0;i<10000;i++) {synchronized (integer) {a++;}}}
}


文章转载自:
http://chinaware.pzdurr.cn
http://canard.pzdurr.cn
http://antipolitical.pzdurr.cn
http://absentee.pzdurr.cn
http://bluethroat.pzdurr.cn
http://balliol.pzdurr.cn
http://balladize.pzdurr.cn
http://benadryl.pzdurr.cn
http://afterdinner.pzdurr.cn
http://auricular.pzdurr.cn
http://cardsharping.pzdurr.cn
http://bristled.pzdurr.cn
http://celanese.pzdurr.cn
http://biomere.pzdurr.cn
http://aragon.pzdurr.cn
http://breechloader.pzdurr.cn
http://catechist.pzdurr.cn
http://brassily.pzdurr.cn
http://accustomed.pzdurr.cn
http://bedaub.pzdurr.cn
http://arsine.pzdurr.cn
http://autocross.pzdurr.cn
http://bailee.pzdurr.cn
http://animatingly.pzdurr.cn
http://cadence.pzdurr.cn
http://airgraph.pzdurr.cn
http://chiropody.pzdurr.cn
http://cairene.pzdurr.cn
http://azurite.pzdurr.cn
http://capitulate.pzdurr.cn
http://www.dtcms.com/a/137258.html

相关文章:

  • Python项目--基于Python的自然语言处理文本摘要系统
  • C++面试考点:类(class)
  • 【开源项目】Excel手撕AI算法深入理解(四):AlphaFold、Autoencoder
  • MySQL 锁机制全景图:分类、粒度与示例一图掌握
  • 每天记录一道Java面试题---day39
  • Web自动化测试的详细流程和步骤
  • shell编程正则表达式与文本处理器
  • 显示模组Bonding IC气泡问题
  • bert项目解析
  • uniapp实现图文聊天功能
  • java-spring笔记
  • HackMyVM Gigachad.
  • 《MySQL基础:了解MySQL周边概念》
  • MySQL 慢查询日志深入分析与工具实战(mysqldumpslow pt-query-digest)
  • 分层式设备控制架构、分布式微服务架构及插件化架构
  • 加密软件:数字时代的隐私守护者
  • 再论火车实验-8
  • 教程:批量提取图片pdf固定位置文字然后保存为新的文件名,基于Python和阿里云的实现方案
  • 大数据面试问答-HBase/ClickHouse
  • javaSE.走进泛型. 泛型类
  • wpf ScaleTransform
  • DeepSeek大模型微调技术PEFT与LoRA详解
  • 大模型落地的关键:如何用 RAG 打造更智能的 AI 搜索——阿里云 AI 搜索开放平台
  • 高等数学A1 期末救济(导数)
  • Vue3 SSR异构渲染引擎:混合现实与时空折叠
  • macOS取证分析——Safari浏览器、Apple Mail数据和Recents数据库
  • 杰弗里·辛顿:深度学习教父
  • Ubuntu服务器性能调优指南:从基础工具到系统稳定性提升
  • 算法升级战报:亚马逊受众定向工具实测点击成本降37%
  • vivado 时钟IP核(MMCM PLL)