最近看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. Linux下的lds链接脚本基础

    转载:http://soft.chinabyte.com/os/104/12255104.shtml   今天在看uboot引导Linux部分,发现要对链接脚本深入了解,才能知道各个目标文件的内存分布 ...

  2. hadoop2 作业执行过程之yarn调度执行

    YARN是hadoop系统上的资源统一管理平台,其主要作用是实现集群资源的统一管理和调度(目前还不完善,只支持粗粒度的CPU和内存的的调配): 它的基本思想是将Mapreduce的jobtracker ...

  3. Java基础知识强化之IO流笔记73:NIO之 Channel

    1. Java NIO的Channel(通道)类似 Stream(流),但又有些不同: 既可以从通道中读取数据,又可以写数据到通道.但流的读写通常是单向的. 通道可以异步地读写. 通道中的数据总是要先 ...

  4. 沈逸老师PHP魔鬼特训笔记(9)--进化

    回到第一课,我们学过PHP母体,了解过解析PHP程序.PHP其实内置了一个web服务器,专门给我们开发测试使用,那么接下来我们要完成的是:生成后创建一个web 服务,在浏览器中可以访问. PHP的母体 ...

  5. 【Linux/Ubuntu学习8】unbuntu 下播放swf文件

    ubuntu是没有自带swf播放器的,需要自己安装. 这次介绍的这个播放器是gnome环境的. 安装: sudo apt-get install swfdec-gnome

  6. PreparedStatement和Statement的区别

    转自:http://blog.sina.com.cn/s/blog_77eba18f01019csh.html 1. PreparedStatement接口继承Statement, PreparedS ...

  7. Spring Data Jpa 详解 (配置篇)

    前言: JPA全称Java Persistence API,即Java持久化API,它为Java开发人员提供了一种对象/关系映射工具来管理Java应用中的关系数据,结合其他ORM的使用,能达到简化开发 ...

  8. javascript组件开发

    最近忙于重构项目,今天周末把在重构中的一些思想记记: 一.javascript的组件开发:基类的封装 由于这次重构项目需要对各种组件进行封装,并且这些组件的实现方式都差不多,所以想到对组件封装一个ba ...

  9. SQLSERVER数据库中的 时间函数

    一.sql server日期时间函数 Sql Server中的日期与时间函数 1.  当前系统日期.时间 select getdate() 2. dateadd  在向指定日期加上一段时间的基础上,返 ...

  10. HTML5移动开发中的meta与link

    meta HTML5移动开发中的一些webkit专属头部标签,能够帮助浏览器更好的解析HTML代码,从而为HTML5移动开发提供更好的前端表现与体验 viewport网页缩放 1 <meta n ...