最近看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#给用户控件添加事件处理程序

    1.首先在usercontrol后台添加如下代码: public partial class MyControl: UserControl { //添加事件代理       public event ...

  2. Socket 之 同步以及异步通信

    用netstat侦听下端口状态 同步通信: 预定义结构体,同步通信没有多线程异步委托回调,所以无需预定义结构体 客户端Client: class Program { static void Main( ...

  3. IOS 从系统图库中获取 图片 并设置为头像

    总会遇到一些应用 要拍照 设置图片之类的的  总结一个方法备用  以后 会 继续拓展到 多图 @interface AddCustomerViewController : UIViewControll ...

  4. TFS 2010 使用手册(四)备份与恢复

    本文参考了http://developer.51cto.com/art/201010/229066.htm的文章. TFS的备份与恢复一直没有很完整的解决方案,要完成一个很好的备份要有很多的手动步骤才 ...

  5. Android小项目练习之一 项目简介

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

  6. No saved view state could be found for the view identifier

    解决方法: javax.faces.application.ViewExpiredException:No saved view state could be found for the view i ...

  7. Hadoop2.0重启脚本

    Hadoop2.0重启脚本 方便重启带ha的集群,写了这个脚本 #/bin/bash sh /opt/zookeeper-3.4.5-cdh4.4.0/bin/zkServer.sh restart ...

  8. python 基础——变量

    变量赋值 1. 把任意类型的变量[名称]赋值给新的变量,总是增加对象引用,而不是创建新的对象 2. 对于list.dict可变类型,操作的都是同一个对象 3. 使用del删除的是该对象的一个引用,只有 ...

  9. 一步步搭建自己的轻量级MVCphp框架-(二)一个国产轻量级框架Amysql源码分析(1)

    这个框架是我一个做PHP的朋友“祥子”介绍给我的,Git地址https://coding.net/u/srar/p/AMP/git 下面从入口文件 总线程 ( index.php )开始进行分析 &l ...

  10. WebSocket在ASP.NET MVC4中的简单实现 (该文章转自网络,经尝试并未实现,请大神指点。)

    WebSocket 规范的目标是在浏览器中实现和服务器端双向通信.双向通信可以拓展浏览器上的应用类型,例如实时的数据推送.游戏.聊天等.有了WebSocket,我们就可以通过持久的浏览器和服务器的连接 ...