How to break double checked lock without volatility

I know that double locking check without mutable variable is unsafe based on this link http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html

class Foo { private Helper helper = null; public Helper getHelper() { if (helper == null) { synchronized(this) { if (helper == null) { helper = new Helper(); } } } return helper; } } 

I want to simulate this situation on my home computer. I have a standard jdk1.7 and multi-core processor. But I can not imitate disturbed behavior. I use this test http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckTest.java which should simulate this. I also create part of my test, but to no avail. You don’t know how to simulate a situation where this double check idiom without volatility is violated? Thus, it returns a partially created Helper class.

+6
source share
1 answer

In x86, this requires either a class with a large number of fields to initialize stores by publication, for example: http://cs.oswego.edu/pipermail/concurrency-interest/2015-January/013861.html

Or you need to execute a compiler to randomize the task scheduler. On non-TSO architectures such as ARM, this can be demonstrated without any tricks, see: http://shipilev.net/blog/2014/safe-public-construction/#_correctness

0
source

Source: https://habr.com/ru/post/980875/


All Articles