memory model
最近看C++11 atomic发现对memory_order很是不理解,memory_order_relaxed/memory_order_consume/memory_order_acquire/memory_order_release/memory_order_acq_rel/
memory_order_seq_cst。这些都是跟memory model有关
关于memory model,对于线程来说,其实是跟编译器相关的。因为我们的编译器在把C++语言翻译成机器代码的时候,会进行各种优化。
我们做C++ GDB的时候,我们会发现,当我们使用-O2优化后,我们再调试的时候,发现程序的执行顺序根本就不是我们写的时候,我们想象的执行顺序。这样优化是有好处的,可以大大提高我们程序的运行速度,但是这样也不是都是好的(volatile类型不建议使用的原因,就是跟这个有关),这些优化都是建立在一个线程上进行的。所以在多线程的时候就要考虑优化造成的问题。
为了不让编译器在做优化的时候,不因为编译器的优化错误,而使程序错误,这就需要acquire/release原语。。
release时,编译器保证不会把写操作移到它后边,否则其他线程就看不到这个修改了;
acquire时,不会把其后的读操作移到它前面,否则读到的就是旧数据。
剩下的情况,编译器就可以自由移动读写操作
而在C++11
memory_order_acquire
Acquire operation:
no reads in the current thread can be reordered before this load.This ensures that all writes in other threads that release the same atomic variable are visible in the current thread
在当前线程中,所有的read操作的乱序,不能出现在这个atomic变量load之前。 (意思就是在当前线程中,编译器或者cpu在优化代码的时候, 不能把读操作的代码出现在这个变量之前)这保证当前线程的writes是可以得到的,而且其他线程应该release这同一个atomic变量
memory_order_release:
Release operation:
no writes in the current thread can be reordered after this store. This ensures that all writes in the current thread are visible in other threads that acquire the same atomic variable.
在当前线程中所有的write不能被reorder到这个atomic变量之后,这保证当前线程的所有write是可以得到的,其他的线程的应该acquire这个atomic变量
memory_order_relaxed:
Relaxed ordering:
there are no constraints on reordering of memory accesses around the atomic variable.
在这个atomic变量上,关于reorder没有约束
memory_order_acq_rel
Acquire-release operation:
no reads in the current thread can be reordered before this load as well as no writes in the current thread can be reordered after this store.The operation is read-modify-write operation. It is ensured that all writes in another threads that release the same atomic variable are visible before the modification and the modification is visible in other threads that acquire the same atomic variable.
memory_order_scq_cst
Sequential ordering.
The operation has the same semantics as acquire-release operation, and additionally has sequentially-consistent operation ordering.
memory_order_consume
Consume operation:
no reads in the current thread dependent on the value currently loaded can be reordered before this load. This ensures that writes to dependent variables in other threads that release the same atomic variable are visible in the current thread. On most platforms, this affects compiler optimization only.
参考资料:
3. Why the "volatile" type class should not be used
4.锁的意义
memory model的更多相关文章
- Java (JVM) Memory Model – Memory Management in Java
原文地址:http://www.journaldev.com/2856/java-jvm-memory-model-memory-management-in-java Understanding JV ...
- 还是说Memory Model,gcc的__sync_synchronize真是太坑爹了
还是说Memory Model,gcc的__sync_synchronize真是太坑爹了! 时间 2012-01-29 03:18:35 IT牛人博客聚合网站 原文 http://www.udpw ...
- Keil中Memory Model和Code Rom Size说明
C51中定义变量时如果省略存储器类型,Keil C51编译系统则会按编译模式SMALL.COMPACT和LARGE所规定的默认存储器类型去指定变量的存储区域,无论什么存储模式都可以声明变量在任何的80 ...
- 当我们在谈论JMM(Java memory model)的时候,我们在谈论些什么
前面几篇中,我们谈论了synchronized.final以及voilate的用法和底层实现,都绕不开一个话题-Java内存模型(java memory model,简称JMM).Java内存模型是保 ...
- 【翻译】go memory model
https://studygolang.com/articles/819 原文链接 Introduction The Go memory model specifies the conditions ...
- 并发研究之Java内存模型(Java Memory Model)
Java内存模型JMM java内存模型定义 上一遍文章我们讲到了CPU缓存一致性以及内存屏障问题.那么Java作为一个跨平台的语言,它的实现要面对不同的底层硬件系统,设计一个中间层模型来屏蔽底层的硬 ...
- java学习:JMM(java memory model)、volatile、synchronized、AtomicXXX理解
一.JMM(java memory model)内存模型 从网上淘来二张图: 上面这张图说的是,在多核CPU的系统中,每个核CPU自带高速缓存,然后计算机主板上也有一块内存-称为主内(即:内存条).工 ...
- CUDA ---- Memory Model
Memory kernel性能高低是不能单纯的从warp的执行上来解释的.比如之前博文涉及到的,将block的维度设置为warp大小的一半会导致load efficiency降低,这个问题无法用war ...
- 11 The Go Memory Model go语言内置模型
The Go Memory Model go语言内置模型 Version of May 31, 2014 Introduction 介绍 Advice 建议 Happens Before 在发生之前 ...
随机推荐
- Android root 原理
Android root 原理 0x00 关于root linux和类Unix系统的最初设计都是针对多用户的操作系统,对于用户权限的管理很非常严格的,而root用户(超级用户)就是整个系统的唯一管理员 ...
- git fetch和git pull之间的区别--转载
原文地址:http://blog.csdn.net/a19881029/article/details/42245955 git fetch和git pull都可以用来更新本地库,它们之间有什么区别呢 ...
- 使用 satis 搭建一个私有的 Composer 包仓库
在我们的日常php开发中可能需要使用大量的composer包,大部份都可以直接使用,但在公司内部总有一小部份包是不能公开的,这时候我们就需要搭建一个公司内部使用的composer仓库,好在compos ...
- 解决python3 不能引入setuptools
1,原理分析: python中的setuptools因为其安全考虑需要ssl模块的支持.如果编译时没有通过ssl测试,就不能安装setuptools. 所以才会出现 Python3/dist-pack ...
- 通过UIBezierPath贝塞尔曲线画圆形、椭圆、矩形
/**创建椭圆形的贝塞尔曲线*/ UIBezierPath *_ovalPath=[UIBezierPath bezierPathWithOvalInRect:CGRectMake(, , , )]; ...
- MySQL(23):事务的隔离级别出现问题之 脏读
1. 脏读 所谓的脏读就是指一个事务读取了另一个事务未提取的数据. 试想一下:a账户要给b账户100元购买商品,如果a账户开启一个事务,执行下面的update语句做了如下转账的工作: update a ...
- ResultSet几种类型的区别
TYPE_FORWARD_ONLY: 默认方式,结果集不能滚动,游标只能向前移动,从第一行移动到最后一行.结果集中的内容与底层数据库生成的结果有关,即生成的结果与查询有关. TYPE_SCROLL_I ...
- ASP.NET页面与IIS底层交互和工作原理详解(第一回)
引言 我查阅过不少Asp.Net的书籍,发现大多数作者都是站在一个比较高的层次上讲解Asp.Net.他们耐心.细致地告诉你如何一步步拖放控件.设置控件属性.编写CodeBehind代码,以实现某个特定 ...
- TaskUtil多线程与定时任务
package com.taoban.util; /** * 执行单次任务或定时任务工具类(用于减少new Thread()和new Timer()的使用) */ public class Tas ...
- jQuery选择器之基本选择器Demo
测试代码: 01-基本选择器.html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" &quo ...