Thread.VolatileRead实施 [英] Thread.VolatileRead Implementation

查看:111
本文介绍了Thread.VolatileRead实施的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看的的 VolatileRead / VolatileWrite 的实施办法(使用反射),我被什么东西感到困惑。

I'm looking at the implementation of the VolatileRead/VolatileWrite methods (using Reflector), and i'm puzzled by something.

这是VolatileRead的实现:

This is the implementation for VolatileRead:

[MethodImpl(MethodImplOptions.NoInlining)]
public static int VolatileRead(ref int address)
{
    int num = address;
    MemoryBarrier();
    return num;
}

怎么会记忆障碍正在读地址的值后放在哪里?这么想的它应该是相反的? (地方阅读的价值,所以任何未决之前写到的地址将被我们做实际的读取时间完成。
同样的事情去VolatileWrite,那里的记忆障碍是地方的的值了分配。这是为什么?
另外,为什么这些方法具有的 NoInlining 的属性?如果他们内联会发生什么?

How come the memory barrier is placed after reading the value of "address"? dosen't it supposed to be the opposite? (place before reading the value, so any pending writes to "address" will be completed by the time we make the actual read. The same thing goes to VolatileWrite, where the memory barrier is place before the assignment of the value. Why is that? Also, why does these methods have the NoInlining attribute? what could happen if they were inlined?

推荐答案

我认为,直到最近。挥发性读取是不是你认为他们是 - 他们不是保证他们获得最新的价值;他们关于确保没有读这就是后来在节目中code被移动之前的这个的读取。这就是规范担保 - 而同样挥发写操作,保证不会早写移到的之后的动荡有一个

I thought that until recently. Volatile reads aren't what you think they are - they're not about guaranteeing that they get the most recent value; they're about making sure that no read which is later in the program code is moved to before this read. That's what the spec guarantees - and likewise for volatile writes, it guarantees that no earlier write is moved to after the volatile one.

您是<一个href=\"https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=94808&wa=wsignin1.0\">not独自在这个怀疑code,但乔·达菲解释它比我能更好:)

You're not alone in suspecting this code, but Joe Duffy explains it better than I can :)

我要回答的是放弃对无锁的编码不是使用像PFX其目的是为了从中隔离我之外。存储模式实在是太辛苦了我 - 我会留给专家,并与事情,我的知道的安全坚守

My answer to this is to give up on lock-free coding other than by using things like PFX which are designed to insulate me from it. The memory model is just too hard for me - I'll leave it to the experts, and stick with things that I know are safe.

有一天,我会更新我的线程的文章反映此事,但我想我需要能够更理智首先讨论它...

One day I'll update my threading article to reflect this, but I think I need to be able to discuss it more sensibly first...

(我不知道无内联的一部分,顺便说一句,我怀疑是内联可以引进这并不意味着要发生挥发左右读/写其他一些优化,但我可以很容易是错误的...)

(I don't know about the no-inlining part, btw. I suspect that inlining could introduce some other optimizations which aren't meant to happen around volatile reads/writes, but I could easily be wrong...)

这篇关于Thread.VolatileRead实施的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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