`

C# memory barrier and the volatile and lock memory barrier attribnute

    博客分类:
  • C#
C# 
阅读更多

Memory barrier is a synchornization mechanism where all operation (read and/or write) must hold until certain conditions occurs (e.g. when some variable finish writing, or finishi reading, or more than one thread reach one point)...

 

C# may or may not have the memory barriers interfaces (this require more research work), however, in this topic, we are goiong to focus on the semantics of memory barrier and how the lock and volatile related to the memory barriers.

 

 

as may be pointted out by this post - Memory Barriers by lock statement

 

Brian Giedon - http://stackoverflow.com/users/158779/brian-gideon 写道
The subject of memory barriers is quite complex. It even trips up the experts from time to time. When we talk about a memory barrier we are really combining two different ideas.

* Acquire fence: A memory barrier in which other reads & writes are not allowed to move before the fence.
* Release fence: A memory barrier in which other reads & writes are not allowed to move after the fence.
A memory barrier that creates only one of two is sometimes called a half-fence. A memory barrier that creates both is sometimes called a full-fence.

The volatile keyword creates half-fences. Reads of volatile fields have acquire semantics while writes have release semantics. That means no instruction can be moved before a read or after a write.

The lock keyword creates full-fences on both boundaries (entry and exit). That means no instruction can be moved either before or after each boundary.

However, all of this moot if we are only concerned with one thread. Ordering, as it is perceived by that thread, is always preserved. In fact, without that fundamental guarentee no program would ever work right. The real issue is with how other threads perceive reads and writes. That is where you need to be concerned.

So to answer your questions:

1. From a single thread's perspective...yes. From another thread's perspective...no.

2. It depends. That might work, but I need to have better understanding of what you are trying to acheive.

3. From another thread's perspective...no. The reads and writes are free to move around within the boundaries of the lock. They just cannot move outside those boundaries. That is why it is important for other threads to also create memory barriers.

 

NOTE:

 

there may be more content following this discusion.

分享到:
评论

相关推荐

    linux memory barrier

    ### Linux Memory Barrier原理详解 #### 一、Memory Barrier概述 在多处理器系统(SMP)中,内存访问顺序和同步成为影响程序正确性和性能的关键因素。Linux 内核中的 Memory Barrier(内存屏障)机制用于控制...

    CPU Cache and Memory Ordering

    – CPU Memory Ordering模型 (Atomic,Reorder,Memory Barrier (Compiler, CPU),Lock Instruction,Load Acquire/Store Release); – 并发程序设计 (实现一个Spinlock,纠正一个Lock-Free Algorithm, Data Race ...

    ARM Cortex™-M Programming Guide to Memory Barrier Instructions

    ARM Cortex™-M系列处理器的内存屏障指令编程指南是一份关于ARM公司推出的Cortex™-M微处理器架构中内存屏障(Memory Barrier)指令的指导性技术文档。这份应用笔记涵盖了ARM Cortex™-M处理器的流水线特性,及其在...

    why memory barrier

    Linux内存屏障(Memory Barrier)是一种用于控制指令执行顺序的机制,它在多处理器(SMP)环境下尤为重要。在理解内存屏障之前,我们首先需要了解CPU缓存以及缓存一致性协议是如何工作的,因为这些都是理解内存屏障...

    volatile源码分析1

    在底层实现上,volatile关键字依赖于JVM的内存模型(Memory Model)和处理器的内存屏障(Memory Barrier)。内存屏障是一种硬件指令,它可以阻止特定类型的指令重排序,并强制将某些操作立即写入内存。在Java中,...

    dispatch_barrier_(a)sync

    1、通过dispatch_barrier_(a)sync添加的block会等待前边所有的block执行完(不包括回调)才执行。 2、在其后添加的block会在dispatch_barrier_(a)sync添加的block执行完之后(不包括回调)再执行; 不同点: 1、...

    Linux-Kernel Memory Model.pdf

    当使用这些宏定义时,虽然可以看作是volatile的memory_order_relaxed访问,但是它们通常只适用于适当的对齐的机器字大小变量。除此之外,`smp_store_release()`和`smp_load_acquire()`宏分别提供了释放型和获取型的...

    CPU Cache and Memory Ordering(修改版)

    - **内存屏障**(Memory Barrier):防止编译器和处理器对指令进行重排序的一种机制。 - **编译器内存屏障**(Compiler Memory Barrier):防止编译器对指令进行重排序。 - **处理器内存屏障**(Processor Memory ...

    barrier_ubuntu和windows共享鼠标键盘的应用

    如果你有2台电脑,一台ubuntu,一台windows,在不断的切换之间,你会感觉到 手也要在不同的鼠标,不同的键盘之间切换很繁琐,那么使用 barrier,可以帮你解这种烦恼,它用来在 ubuntu 和 windows 之间 定义一个电脑...

    barrier.zip (synergy免费版)

    **标题解析:** "barrier.zip (synergy免费版)" 提供的是一个名为"barrier"的软件压缩包,该软件是Synergy的免费版本。Synergy是一款强大的跨平台工具,允许用户通过单一的鼠标和键盘控制多台计算机,实现多设备间的...

    Carbon dioxide conversion synergistically activated by dielectric barrier discharge plasma and the CsPbBr3@TiO2 photocatalyst.pdf

    在众多CO2转化技术中,利用电介质阻挡放电(Dielectric Barrier Discharge,DBD)等离子体与光催化剂的协同作用,提供了一条新的技术路线。DBD等离子体是一种非热等离子体,能够在相对较低的温度下产生高能电子、...

    mpi_barrier.rar_The Work

    MPI Barrier是Message Passing Interface (MPI)库中的一个关键同步原语,它在并行计算中起着至关重要的作用。MPI_BARRIER函数允许所有参与进程在继续执行下一个阶段之前等待彼此到达一个预定义的同步点,确保所有...

    Effects of Gibbs free energy changes and energy barrier on mechanochemical synthesis of LiMn2O4

    Effects of Gibbs free energy changes and energy barrier on mechanochemical synthesis of LiMn2O4,赵中伟,欧阳全胜,本研究以KMnO4和MnSO4•H2O粉末为Mn源,LiOH•H2O粉末为Li源,在室温条件下粉磨合成LiMn2O4...

    barrier windows 安装文件

    barrier windows 安装文件

    Trade off and Optimization in Analog CMOS Design

    Measured data that validates the inclusion of important small-geometry effects like velocity saturation, vertical-field mobility reduction, drain-induced barrier lowering, and inversion-level ...

    乱序执行和内存屏障.pdf

    mb garanties that all loads and stores before the barrier will be visible to all processors before any loads and stores after the barrier. 在编程中,内存屏障通常用于多线程编程中,用于保证多个线程...

    C++中volatile关键字及常见的误解总结

    C/C++ 中的 volatile 关键字和 const 对应,用来修饰变量,通常用于建立语言级别的 memory barrier。这是 BS 在 “The C++ Programming Language” 对 volatile 修饰词的说明: A volatile specifier is a hin

    内存栅栏和volatile关键字1

    然而,volatile并不能阻止CPU对其他非volatile变量的重排序和缓存策略,因此在某些场景下,还需要使用内存栅栏(Memory Barrier)来确保数据同步。内存栅栏是一种指令,它强制在栅栏之前和之后的指令不发生交叉执行...

Global site tag (gtag.js) - Google Analytics