内存屏障发电机 [英] Memory barrier generators

查看:114
本文介绍了内存屏障发电机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阿尔巴哈利的线程教程,以下是提到的内存屏障发电机:

Reading Joseph Albahari's threading tutorial, the following are mentioned as generators of memory barriers:


  • C#的锁定语句( Monitor.Enter / Monitor.Exit

  • 互锁类的所有方法

  • 使用线程池的异步回调 - 包括异步委托,APM回调和任务延续

  • 设置和等待中的信令结构

  • 任何依赖于信号,如启动或等待任务

  • C#'s lock statement (Monitor.Enter/Monitor.Exit)
  • All methods on the Interlocked class
  • Asynchronous callbacks that use the thread pool — these include asynchronous delegates, APM callbacks, and Task continuations
  • Setting and waiting on a signaling construct
  • Anything that relies on signaling, such as starting or waiting on a Task

此外,汉斯帕桑特和布赖恩·吉迪恩<一个href=\"http://stackoverflow.com/questions/6574389/thread-safe-usage-of-lock-helpers-concerning-memory-barriers\">added以下(其中假设已经没有适合的previous类别之一):

In addition, Hans Passant and Brian Gideon added the following (assuming none of which already fits into one of the previous categories):


  • 启动或唤醒线程

  • 上下文切换

  • Thread.sleep代码()

  • Starting or waking up a thread
  • Context switch
  • Thread.Sleep()

我在想,如果这个名单是完成(如果完整列表甚至可以做几乎)

I was wondering if this list was complete (if a complete list could even be practically made)

修改补充建议:


  • 挥发性(读数意味着某个acquire栅栏,写作意味着释放栅栏)

推荐答案

下面是我对这个问题,并试图在一个答案提供了准完整列表。要是遇到任何别人跑我会不时编辑我的回答时间。

Here is my take on the subject and to attempt to provide a quasi-complete list in one answer. If I run across any others I will edit my answer from time to time.

这是一般商定机制造成隐性障碍:

Mechanisms that are generally agreed upon to cause implicit barriers:


  • 所有显示器类的方法,包括C#关键字锁定

  • 所有互锁类的方法。

  • 所有挥发性类的方法(.NET 4.5 +)。

  • 自旋锁方法,包括输入退出

  • 的Thread.join

  • Thread.VolatileRead Thread.VolatileWrite

  • Thread.MemoryBarrier

  • 挥发性关键字。

  • 凡是启动一个线程,或导致委托给另一个线程上执行的,包括 QueueUserWorkItem Task.Factory.StartNew Thread.Start ,编译器提供的的BeginInvoke 方法等。

  • 使用的信令机制,如的ManualResetEvent 的AutoResetEvent CountdownEvent 旗语壁垒

  • 使用编组操作,如 Control.Invoke Dispatcher.Invoke 的SynchronizationContext。帖子

  • All Monitor class methods including the C# keyword lock
  • All Interlocked class methods.
  • All Volatile class methods (.NET 4.5+).
  • Most SpinLock methods including Enter and Exit.
  • Thread.Join
  • Thread.VolatileRead and Thread.VolatileWrite
  • Thread.MemoryBarrier
  • The volatile keyword.
  • Anything that starts a thread or causes a delegate to execute on another thread including QueueUserWorkItem, Task.Factory.StartNew, Thread.Start, compiler supplied BeginInvoke methods, etc.
  • Using a signaling mechanism such as ManualResetEvent, AutoResetEvent, CountdownEvent, Semaphore, Barrier, etc.
  • Using marshaling operations such as Control.Invoke, Dispatcher.Invoke, SynchronizationContext.Post, etc.

这是猜测(但肯定不知道)的机制导致隐性障碍:

Mechanisms that are speculated (but not known for certain) to cause implicit barriers:


  • Thread.sleep代码(由我自己,并提出可能还有其他原因的事实,code这显示出内存屏障问题,可以固定使用此方法)

  • Thread.Yield

  • Thread.SpinWait

  • 懒&LT; T&GT; 这取决于 LazyThreadSafetyMode 指定

  • Thread.Sleep (proposed by myself and possibly others due to the fact that code which exhibits a memory barrier problem can be fixed with this method)
  • Thread.Yield
  • Thread.SpinWait
  • Lazy<T> depending on which LazyThreadSafetyMode is specified

其他值得注意的提到:


  • 默认在C#中添加和删除处理事件,因为它们使用锁定 Interlocked.CompareExchange

  • x86的商店有释放栅栏语义

  • 的CLI微软的实行对写入释放栅栏语义尽管在ECMA规范不强制它。

  • MarshalByRefObject的似乎燮preSS某些优化在子类中这可能使它看起来好像一个内隐记忆障碍为present。由于汉斯帕桑特了解发现这个并将其推向了我的注意。 1

  • Default add and remove handlers for events in C# since they use lock or Interlocked.CompareExchange.
  • x86 stores have release fence semantics
  • Microsoft's implemenation of the CLI has release fence semantics on writes despite the fact that the ECMA specification does not mandate it.
  • MarshalByRefObject seems to suppress certain optimizations in subclasses which may make it appear as if an implicit memory barrier were present. Thanks to Hans Passant for discovering this and bringing it to my attention.1

1 这就解释了为什么的BackgroundWorker 正常工作,而无需挥发性上为 CancellationPending 属性基础字段。

1This explains why BackgroundWorker works correctly without having volatile on the underlying field for the CancellationPending property.

这篇关于内存屏障发电机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆