简体   繁体   English

x86 / x64架构是否能够抵御不安全的发布?

[英]Are x86/x64 architectures resistant to unsafe publication?

I have observed, that unsafe publication of objects in JVM 1.5+ is very unlikely to cause any trouble, regardless of JVM specs stating that it is not guaranteed that such objects will be visible between threads. 我观察到,JVM 1.5+中不安全的对象发布不太可能造成任何麻烦,无论JVM规范如何都不能保证这些对象在线程之间是可见的。

While looking around on the internet I have found this: http://forum.springsource.org/archive/index.php/t-60676.html Person with nick " al0 " claims, that "[...] it is very unlikely to meet such behaviour on x86/x64 based machines, but on HP PA-RISC or IBM Power... based computers (for example AS400) it is much more likely". 在互联网上环顾四周时,我发现了这个: http//forum.springsource.org/archive/index.php/t-60676.html 有人昵称“ al0 ”的人说,“[...]它非常不太可能在基于x86 / x64的计算机上遇到此类行为,但在HP PA-RISC或基于IBM Power ...的计算机(例如AS400)上,它更可能“。

Are x86/x64 architectures resistant to unsafe publication? x86 / x64架构是否能够抵御不安全的发布? How come? 怎么会?

x86 processors are somewhat resistant to unsafe publication. x86处理器对不安全的出版物有一定的抵抗力。 In particular, as long as one thread is only writing to shared memory and another is only reading, the processor treats all loads and stores as if the memory locations had Java volatile semantics. 特别是,只要一个线程只写入共享内存而另一个线程只读取, 处理器就会将所有加载和存储视为内存位置具有Java volatile语义。 Writes are never reordered past writes, and reads are never reordered past reads, so the reading thread always sees writes in the correct order 写入永远不会在写入之后重新排序,并且读取从不会在读取之后重新排序,因此读取线程始终以正确的顺序查看写入

However: 然而:

  • writes can be moved after a read that comes later in the program. 在程序中稍后的读取之后,可以移动写入。 In this sense, volatile is still stronger than what the x86 promises 从这个意义上讲, volatile仍然比x86承诺的更强大

  • this only covers what the processor can do to your code. 这仅涵盖处理器可以对您的代码执行的操作。 The virtual machine can still reorder the code as it wishes. 虚拟机仍然可以按照自己的意愿重新排序代码。 For example, it could rewrite xb = 4; ya = 5; 例如,它可以重写xb = 4; ya = 5; xb = 4; ya = 5; to ya = 4; xb = 5 ya = 4; xb = 5 ya = 4; xb = 5 . ya = 4; xb = 5

Such a decision can be taken based on many different factors: choosing which parts of the code will be JIT-compiled, inlining, scheduling... So even on processors with strong memory ordering, unsafe publication remains unsafe. 这样的决定可以基于许多不同的因素:选择代码的哪些部分将被JIT编译,内联,调度......所以即使在具有强大内存排序的处理器上,不安全的发布仍然是不安全的。

I don't want to speculate if the problem is more or less likely to occur on x86/x64 CPUs. 我不想推测x86 / x64 CPU上是否出现问题的可能性更大或更小。 Claiming that it is very unlikely to see this behaviour on x86/x64 CPUs is wrong . 声称不太可能在x86 / x64 CPU上看到这种行为是错误的

Niklas Schlimm has written an article about this in DZone with a working example to demonstrate the incorrect behaviour if a variable with multi-threaded access is not declared volatile. Niklas Schlimm在DZone中写了一篇关于此内容的文章,其中有一个工作示例,用于演示如果多线程访问变量未声明为volatile的错误行为。 He writes in the article that he can only reproduce the behaviour with the server VM, but for me, the test fails with the client VM as well (Oracle Java 7, Intel x64 Mobile CPU). 他在文章中写道,他只能重现服务器虚拟机的行为,但对我来说,测试也失败了客户端虚拟机(Oracle Java 7,英特尔x64移动CPU)。 It is important to remember that it depends on at least the exact version of the Java VM and the CPU model if the behaviour is reproducable at all or perhaps even only sporadic. 重要的是要记住,如果行为完全可再现,甚至可能只是零星的,它至少取决于Java VM和CPU模型的确切版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM