我正在使用 .Net 4.5(预览版...4 对于这个问题来说就可以了)。我正在做线程工作。
根据我的研究,我知道 x86 CPU 具有强大的内存模型,这意味着写入不会被重新排序。这使得释放锁变得安全。对于具有较弱内存模型的 Itanium CPU 来说,情况并非如此。
我了解易失性、内存屏障和执行重排序原则。
理想情况下,如果 CPU 是 Itanium,则在关键点插入内存屏障,但如果 CPU 是 x86,则不需要。是否可以动态地执行此操作,就像 JIT 处理的运行时编译器指令一样?
如果没有,我意识到我将需要为两个平台进行单独的构建。在这种情况下,在没有 2 组 C# 文件的情况下,最优雅的方法是什么,而只是简单地更改目标?
I'm using .Net 4.5 (preview... 4 is fine for the purposes of this question). I'm doing threading work.
Based on my studies, I know that x86 CPUs have a strong memory model, which means writes won't be reordered. This makes releasing locks safe. This is not true for Itanium CPUs, which have a weak memory model.
I understand volatile, memory barriers, and execution reordering principles.
What I need ideally is to insert memory barriers at key points if the CPU is Itanium, but not if it's x86. Is it possible to do this dynamically, as in have runtime compiler directives that the JIT processes?
If not, I realise I will need to have separate builds for the two platforms. In that case, what is the most elegant way to do this without having 2 sets of C# files, but rather simply changing the target?
发布评论
评论(2)
回答你的主要问题;我认为目前不可能将 CIL 指令有条件地编译为基于平台的机器指令(除了 JIT 编译器中内置的指令)。
从一组源代码创建两个(或更多)版本的主要工具仍然是 预处理器指令。
In answer to your main question; I don't think it's currently possible to have CIL instructions conditionally compiled to machine instructions based on platform (other than what's been baked into the JIT compiler).
Your primary tool for creating two (or more) builds from one set of source is still preprocessor directives.
我不知道这是否对您有帮助,但是这个答案中描述了几个选项:
在另一个答案中,有人提供了以下示例代码(来自 Paint.NET)的链接来确定操作系统架构。您可以进行细微调整以包含
IA64
检查:I have no idea if this will help you or not, but there are a couple of options described in this answer:
In another answer, somebody provided a link to the following sample code (from Paint.NET) to determine OS architecture. You could make a minor adjustment to include the
IA64
check: