了解非阻塞线程同步和Thread.MemoryBarrier [英] Understanding non blocking thread synchronization and Thread.MemoryBarrier

查看:153
本文介绍了了解非阻塞线程同步和Thread.MemoryBarrier的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此线程在线图书: http://www.albahari.com/threading/part4。 ASPX

孤单的一个例子 Thread.MemoryBarrier()

   class Foo
{
  int _answer;
  bool _complete;

  void A()
  {
    _answer = 123;
    Thread.MemoryBarrier();    // Barrier 1
    _complete = true;
    Thread.MemoryBarrier();    // Barrier 2
  }

  void B()
  {
    Thread.MemoryBarrier();    // Barrier 3
    if (_complete)
    {
      Thread.MemoryBarrier();       // Barrier 4
      Console.WriteLine (_answer);
    }
  }
}

我们得到了一个讨论是否有任何线程阻塞怎么回事不?

We got a discussion whether there is any thread blocking going on or not?

即时通讯思想有一些,特别是考虑到

Im thinking there is some, especially given that

一个完整的围栏是以约十纳秒的2010时代的桌面上。

A full fence takes around ten nanoseconds on a 2010-era desktop.

在另一方面,全栅栏只应该禁用指令reodering和缓存它通过它的声音没有资格作为线程阻塞,(不像锁定,其中明确其线程等待其他后再继续,在此期间被阻止),解除锁定

On other hand, full fence is only supposed to disable instructions reodering and caching which by its sound doesn't qualify as thread blocking, (unlike lock where its clear that thread waits for other to release lock before it continues, and is blocked during that time)

关于线程阻塞状态。 IM谈论不是在线程是否投入方面的阻止状态与否,而是是否有一些线程同步发生,这意味着一个线程不能,而其他不让它做运行这样,通过MemoryBarrier在这种情况下,装置

About that thread 'block state'. im talking not in terms of whether thread is put into blocked state or not, but whether there is some thread synchronization happening, which means one thread is not able to run while other isn't letting it to do so, by means of MemoryBarrier in this case.

另外我还想得到清楚的了解每一个障碍实现什么。例如屏障2 - 究竟它提供清新的保证,它是怎么连接到隔离3?如果有人将详细解释什么每个屏障这里的目的(什么可能出问题,如果1或2或3或4个都没有了)我想身份证提高我的这种认识很大。

Also Id like to get clear understanding what each barrier achieves. For example Barrier 2 - how exactly it provides freshness guarantee and how is it connected to barrier 3? If someone would explain in detail whats each barrier purpose here( what could possibly go wrong if 1 or 2 or 3 or 4 weren't there) I think id improve my understanding of this greatly.

编辑:的大多是清楚现在是什么1,2,3做。但是什么4做了3不目前还不清楚。

its mostly clear now what 1, 2, and 3 do. However what 4 does that 3 doesn't is still unclear.

推荐答案

这说明需要时间来执行的事实确实的没有的意味着一个线程被阻塞。一个线程被阻塞时,它是专门置于阻塞状态,其中 MemoryBarrier()没有做。

The fact that instructions take time to execute does not imply that a thread is blocked. A thread is blocked when it is specifically put into a blocked state, which MemoryBarrier() does not do.

这其实prevent指令重新排序和缓存刷新需要时间,因为他们必须等待缓存再次成为连贯的处理器指令。在此期间,该线程仍然被认为是运行

The processor instructions that actually prevent instruction reordering and cache flushing take time, because they must wait for the caches to become coherent again. During that time, the thread is still considered running.

更新:那么让我们来看看什么是实际发生的例子,以及每个内存屏障实际执行

Update: So let's take a look at what's actually happening in the example, and what each memory barrier actually does.

作为链接说,1和4确保正确答案就制成了。这是因为1确保了答案被冲洗到存储器,和4保证读高速缓存检索所述变量之前进行冲洗。

As the link says, 1 and 4 ensure that the correct answers are produced. That's because 1 ensures that the answers are flushed into memory, and 4 ensures that the read caches are flushed prior to retrieving the variables.

2和3确保如果 A 运行,再 B 总是的打印的答案。屏障2,确保该写被刷新到内存和阻隔3保证读cahces测试之前,将会刷新 _complete 的价值。

2 and 3 ensure that if A runs first, then B will always print the answers. Barrier 2 ensures that the write of true is flushed to memory, and barrier 3 ensures that the read cahces are flushed before testing _complete's value.

的高速缓存和内存冲洗应该是足够清晰,所以让我们来看看指令重新排序。方式,编译器,CLR和CPU知道他们可以重新排序指令是通过分析一组中的序列指令​​。当他们看到一个序列中间的屏障指令的 的,他们知道指令的不能跨越的界限的举动。即确保除了缓存新鲜度,所述指令发生在正确的顺序

The cache and memory flushing should be clear enough, so let's look at instruction reordering. The way the compiler, CLR and CPU know they can reorder instructions is by analyzing a set of instructions in sequence. When they see the barrier instruction in the middle of a sequence, they know that instructions can't move across that boundary. That ensures that in addition to cache freshness, the instructions occur in the correct order.

这篇关于了解非阻塞线程同步和Thread.MemoryBarrier的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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