c# - 半栅栏和全栅栏?

标签 c# .net multithreading

我一直在阅读Full fences可以防止任何类型的指令在该围栏周围重新排序或缓存(通过memoryBarrier)

然后,我阅读了有关生成“半栅栏”的volatile的信息:

The volatile keyword instructs the compiler to generate an acquire-fence on every read from that field, and a release-fence on every write to that field.



获得栅栏

An acquire-fence prevents other reads/writes from being moved before the fence;



释放栅栏

A release-fence prevents other reads/writes from being moved after the fence.



有人可以用简单的英语给我解释这两个句子吗?

(篱笆在哪里?)

编辑

在这里得到一些答案之后-我已经绘制了一张可以帮助所有人的图纸-我认为。

http://i.stack.imgur.com/A5F7P.jpg

最佳答案

您所指的措辞看起来像我经常使用的措辞。规范虽然这样说:

  • 对volatile字段的读取称为volatile读取。 volatile 读取具有“获取语义”。也就是说,保证在指令序列中发生在对内存的任何引用之前。
  • volatile 字段的写入称为 volatile 写入。 volatile 写入具有“释放语义”。即,保证在指令序列中的写入指令之前的任何存储器引用之后发生。

  • 但是,我通常会使用您在问题中引用的措辞,因为我想将重点放在可以移动指令的事实上。您引用的措辞与规范是等效的。

    我将举几个例子。在这些示例中,我将使用一种特殊的符号,该符号使用↑箭头指示释放围栏,并使用↓箭头指示获取围栏。不允许任何其他指令向下 float 超过↑箭头或向上 float 超过↓箭头。将箭头视为排斥一切。

    考虑下面的代码。
    static int x = 0;
    static int y = 0;
    
    static void Main()
    {
      x++
      y++;
    }
    

    将其重写以显示各个指令将如下所示。
    static void Main()
    {
      read x into register1
      increment register1
      write register1 into x
      read y into register1
      increment register1
      write register1 into y
    }
    

    现在,由于在此示例中没有内存障碍,因此C#编译器,JIT编译器或硬件可以自由地以许多不同的方式对其进行优化,只要执​​行线程感知到的逻辑顺序与物理顺序一致即可。这是一种这样的优化。注意如何交换对xy的读写。
    static void Main()
    {
      read y into register1
      read x into register2
      increment register1
      increment register2
      write register1 into y
      write register2 into x
    }
    

    现在,这次将这些变量更改为volatile。我将使用箭头符号标记内存障碍。注意如何保留对xy的读写顺序。这是因为指令无法越过我们的障碍(以↓和↑箭头表示)。现在,这很重要。注意,仍然允许x指令的增量和写入向下 float ,而y的读取向上 float 。这仍然是有效的,因为我们使用的是半栅栏。
    static volatile int x = 0;
    static volatile int y = 0;
    
    static void Main()
    {
      read x into register1
      ↓    // volatile read
      read y into register2
      ↓    // volatile read
      increment register1
      increment register2
      ↑    // volatile write
      write register1 into x
      ↑    // volatile write
      write register2 into y
    }
    

    这是一个非常琐碎的例子。看看我的答案here,了解volatile如何在双重检查模式中有所作为的一个重要例子。我使用了与此处相同的箭头符号,以使可视化变得容易。

    现在,我们还有Thread.MemoryBarrier方法可以使用。它会生成一个完整的围栏。因此,如果我们使用箭头符号,则可以直观地看到它的工作方式。

    考虑这个例子。
    static int x = 0;
    static int y = 0;
    
    static void Main
    {
      x++;
      Thread.MemoryBarrier();
      y++;
    }
    

    如果我们要像以前一样显示各个说明,则该内容应如下所示。注意,现在完全禁止了指令移动。在不损害指令逻辑顺序的情况下,实际上没有其他方法可以执行此操作。
    static void Main()
    {
      read x into register1
      increment register1
      write register1 into x
      ↑    // Thread.MemoryBarrier
      ↓    // Thread.MemoryBarrier
      read y into register1
      increment register1
      write register1 into y
    }
    

    好,再举一个例子。这次让我们使用VB.NET。 VB.NET没有volatile关键字。那么,我们如何模仿VB.NET中的 volatile 读取?我们将使用Thread.MemoryBarrier .1
    Public Function VolatileRead(ByRef address as Integer) as Integer
      Dim local = address
      Thread.MemoryBarrier()
      Return local
    End Function
    

    这就是我们用箭头表示的样子。
    Public Function VolatileRead(ByRef address as Integer) as Integer
      read address into register1
      ↑    // Thread.MemoryBarrier
      ↓    // Thread.MemoryBarrier
      return register1
    End Function
    

    重要的是要注意,由于我们要模仿 volatile 读取,因此必须在实际读取之后放置对Thread.MemoryBarrier的调用。不要陷入这样的陷阱: volatile 读意味着“新读”,而 volatile 写意味着“已提交”。这不是它的工作方式,当然也不是该规范描述的内容。

    更新:

    引用图片。

    wait! I am verifing that all the Writes are finished!





    wait! I am verifying that all the consumers have got the current value!



    这就是我在说的陷阱。这些陈述并不完全准确。是的,在硬件级别实现的内存屏障可以使高速缓存一致性行同步,结果,上面的陈述对于发生的情况可能有些准确。但是,volatile只不过限制了指令的移动。规范说没有关于从内存加载值或将其存储到内存屏障所在位置的

    1当然已经有内置的Thread.VolatileRead了。您会注意到它的实现与我在此处所做的完全相同。

    关于c# - 半栅栏和全栅栏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10589654/

    相关文章:

    .net - 如何在 3rd 方 .NET 应用程序中自动选择日历日期?

    multithreading - 关于启动大型多线程编程项目的建议

    c# - 绑定(bind)组合框以显示整数值

    c# - ASP MVC 在局部 View 中定义部分

    c# - 为 HttpClient 中的 Get 操作显式设置内容类型 header

    .net - Visual Studio 发布选择错误的 web.config 转换

    c# - 如何检查文件头结构以识别 MSI 文件

    c# - 为什么在 MVC 1 中工作的 ajax 在 MVC3 中不能工作?

    python - 如何中断 python 中的阻塞方法?

    linux - 关于pthread_mutex_lock和pthread_mutex_unlock的一些问题