c++11内存模型 memory_order整理笔记

一、c++11 std::atomic 原子操作提供的参数

typedef enum memory_order {
    memory_order_relaxed,
    memory_order_consume,
    memory_order_acquire,
    memory_order_release,
    memory_order_acq_rel,
    memory_order_seq_cst
    } memory_order;

二、原子操作的两大关键作用;第二个是大部分人会忽视的用途;

An atomic operation has two key properties that help you use multiple threads to correctly manipulate an object without using mutex locks.

  • Because an atomic operation is indivisible, a second atomic operation on the same object from a different thread can obtain the object's state only before or after the first atomic operation.

  • Based on its memory_order argument, an atomic operation establishes ordering requirements for the visibility of the effects of other atomic operations in the same thread. Consequently, it inhibits compiler optimizations that violate the ordering requirements.

以上摘自:

https://docs.microsoft.com/en-us/cpp/standard-library/atomic?view=vs-2019

三、关于memory_order的说明和使用

Supplies symbolic names for synchronization operations on memory locations. These operations affect how assignments in one thread become visible in another.

个人理解:

提供对内存操作同步的符号指示;这些说明符号指示,影响一个线程中多个写操作在另外一个线程中的可见性;一个线程中的多个只读操作的重排序,对其他线程并不会有影响;

关于memory_order的含义:

如何理解 C++11 的六种 memory order? - zlilegion的回答 - 知乎 https://www.zhihu.com/question/24301047/answer/85844428

 

如何理解 C++11 的六种 memory order? - 陈文礼的回答 - 知乎 https://www.zhihu.com/question/24301047/answer/1193956492

我还是比较认可这个观点的;就是

C++11所规定的这6种模式,其实并不是限制(或者规定)两个线程该怎样同步执行,而是在规定一个线程内的指令该怎样执行

“是的,我知道这部分的文档(规定)以及给出的例子里面,满屏都是多线程。但是其实讨论的是单线程的问题。更为准确地说,是在讨论单线程内的指令执行顺序对于多线程的影响的问题。”

 

四、内存栅栏和安全的单例实现

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值