最近看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.

参考资料:

1 . 浅谈Memory Reordering

2. 透过LINUX内核看无锁编程

3. Why the "volatile" type class should not be used

4.锁的意义

memory model的更多相关文章

  1. Java (JVM) Memory Model – Memory Management in Java

    原文地址:http://www.journaldev.com/2856/java-jvm-memory-model-memory-management-in-java Understanding JV ...

  2. 还是说Memory Model,gcc的__sync_synchronize真是太坑爹了

    还是说Memory Model,gcc的__sync_synchronize真是太坑爹了! 时间 2012-01-29 03:18:35  IT牛人博客聚合网站 原文  http://www.udpw ...

  3. Keil中Memory Model和Code Rom Size说明

    C51中定义变量时如果省略存储器类型,Keil C51编译系统则会按编译模式SMALL.COMPACT和LARGE所规定的默认存储器类型去指定变量的存储区域,无论什么存储模式都可以声明变量在任何的80 ...

  4. 当我们在谈论JMM(Java memory model)的时候,我们在谈论些什么

    前面几篇中,我们谈论了synchronized.final以及voilate的用法和底层实现,都绕不开一个话题-Java内存模型(java memory model,简称JMM).Java内存模型是保 ...

  5. 【翻译】go memory model

    https://studygolang.com/articles/819 原文链接 Introduction The Go memory model specifies the conditions ...

  6. 并发研究之Java内存模型(Java Memory Model)

    Java内存模型JMM java内存模型定义 上一遍文章我们讲到了CPU缓存一致性以及内存屏障问题.那么Java作为一个跨平台的语言,它的实现要面对不同的底层硬件系统,设计一个中间层模型来屏蔽底层的硬 ...

  7. java学习:JMM(java memory model)、volatile、synchronized、AtomicXXX理解

    一.JMM(java memory model)内存模型 从网上淘来二张图: 上面这张图说的是,在多核CPU的系统中,每个核CPU自带高速缓存,然后计算机主板上也有一块内存-称为主内(即:内存条).工 ...

  8. CUDA ---- Memory Model

    Memory kernel性能高低是不能单纯的从warp的执行上来解释的.比如之前博文涉及到的,将block的维度设置为warp大小的一半会导致load efficiency降低,这个问题无法用war ...

  9. 11 The Go Memory Model go语言内置模型

    The Go Memory Model go语言内置模型 Version of May 31, 2014 Introduction 介绍 Advice 建议 Happens Before 在发生之前 ...

随机推荐

  1. 关于c中的%x及其它格式化符

    原文:http://blog.csdn.net/lincyang/article/details/6252443 格式化: %x表示按16进制输出:int a = 16;%02x:输出10:%03x: ...

  2. Windows下memcache安装使用

    Windows下Memcache安装 随着时间的推移,网上现在能找到的在 Windows下安装 Memcache 的文档大多已经过时.雪峰这里再简要介绍一下当下最新版的安装和配置方法. Memcach ...

  3. [Arduino] 逗号分隔文本到数组的两种方法

    以下是今日练习通过逗号来分割字符数组/字符串的2个例子和方法" 1.通过indexOf函数 /* *Splitsplit sketch *split a comma-separated st ...

  4. Nodejs新建博客练习(一)安装express并新建项目

    安装express npm install -g express-generator 新建工程 express blog //新建项目 cd blog && npm install / ...

  5. 【阿里云产品公测】PTS测试 SLB+ECS+RDS组合的DZ论坛负载极限压力,100并发2000页

    作者:阿里云用户woaj01 ​​环境介绍: 1.ECS:1核 1G 5M 杭州 2.RDS:240M  5G  杭州内网 3.SLB:私网实例​ 配置测试环境: 测试脚本: 1.生成参数文件,我的方 ...

  6. Android小项目之五 splash动画效果

    ------- 源自梦想.永远是你IT事业的好友.只是勇敢地说出我学到! ---------- 按惯例,写在前面的:可能在学习Android的过程中,大家会和我一样,学习过大量的基础知识,很多的知识点 ...

  7. iOS UILable的一些用法

    1.按钮上的文字添加下划线 问题:实现下图中右侧的按钮文字效果 代码(绿色为按钮文字加下划线方法): [MyTools createMyImageview:topEditView frame:CGRe ...

  8. Django升级1.6之后出现“Bad Request (400)”错误的解决方案

    Django从1.4升级到1.6之后发现之前的网站都无法访问了,会出现“Bad Request (400)”的错误,搜了半天终于找到了解决办法. 解决方法很简单: 在settings.py里面添加: ...

  9. HTML--内联元素与块级元素

    >>内联元素(inline element) a,span,input,select,label,img,textarea,sub,sup,li,i,small,strong,em,b,b ...

  10. 日期类型的input元素设置默认值为当天

    html文件:<input name="" type="date" value="" id="datePicker" ...