Questions tagged [double-checked-locking]

Double-checked locking is a software design pattern used to reduce the overhead of acquiring a lock by first testing the locking criterion without actually acquiring the lock.

From Wikipedia:

In software engineering, double-checked locking (also known as "double-checked locking optimization"1) is a software design pattern used to reduce the overhead of acquiring a lock by first testing the locking criterion (the "lock hint") without actually acquiring the lock. Only if the locking criterion check indicates that locking is required does the actual locking logic proceed.

The pattern, when implemented in some language/hardware combinations, can be unsafe. At times, it can be considered an anti-pattern.[2]

It is typically used to reduce locking overhead when implementing "lazy initialization" in a multi-threaded environment, especially as part of the Singleton pattern. Lazy initialization avoids initializing a value until the first time it is accessed.

143 questions
3
votes
5 answers

Potential problems in Double Checked locking pattern in Singleton class

I believe the below Singleton class that I have wrote is Thread Safe. The double-checked locking pattern might run into problems in some circumstances apparently (I've seen people warn against it, though it was a while ago so I'd just be Googling…
user1813228
3
votes
1 answer

Double-Checked Locking disappeared from checkstyle - why?

From the release notes, checkstyle removed the Double-Checked locking check. I'm having a hard time understanding why. They replied this not only in the release notes but also in the issue tracker: Removed the DoubleCheckedLocking check, as in Java…
Thiago
  • 2,238
  • 4
  • 29
  • 42
3
votes
1 answer

Java Double check locking, does this code work?

I have read The "Double-Checked Locking is Broken" Declaration. But I wonder if i create the object by a function, will that be ok? class Foo { private Helper helper = null; public Helper getHelper() { if (helper == null) …
jinghui70
  • 31
  • 2
2
votes
3 answers

Is this code Double Checked Locking safe?

I am looking at some code in our app that I think may be encountering a case of "Double-checked locking". I have written some sample code that is similar to what we do. Can anyone see how this can be experiencing double-checked locking? Or is this…
Aishwar
  • 9,284
  • 10
  • 59
  • 80
2
votes
4 answers

Is DCL (double-checked locking) thread-safe when implemented by ‘Sequentially Consistent Atomics’ semantics in C++?

Here is a piece of code that is DCL (double-checked locking) implemented by ‘Sequentially Consistent Atomics’ semantics in C++. The code is as follows: std :: atomic Singleton :: m_instance; std :: mutex Singleton ::…
BobJao
  • 79
  • 1
  • 6
2
votes
1 answer

Why is double-checked locking not used properly in the implementation of System.console() in the openJDK?

In the openJDK source code, the System.console() was implemented as such: private static volatile Console cons = null; /** * Returns the unique {@link java.io.Console Console} object associated * with the current Java virtual machine, if any. * …
Rui
  • 3,454
  • 6
  • 37
  • 70
2
votes
4 answers

Which implementation for lazy singleton whose initialialization might fail?

Imagine you have a static no-argument method which is idempotent and always returns the same value, and may throw a checked exception, like so: class Foo { public static Pi bar() throws Baz { getPi(); } // gets Pi, may throw } Now this is a good…
BeeOnRope
  • 60,350
  • 16
  • 207
  • 386
2
votes
1 answer

Double-checked locking for initializing a state on disk

I'm trying to write a piece of code that allows me to install a library (i.e. download an archive from a remote endpoint and uncompress it on disk) once per JVM (may have several JVMs per machine but it's not the point of the question). I'm aware of…
Dici
  • 25,226
  • 7
  • 41
  • 82
2
votes
2 answers

Check if file exists on server and return that file content

I want to check if the file exist on server on multithread environment and if exists return that file content diractly or download from my s3 service server. My code like this: final Object lock = new Object(); File file = new File("/file/path"); if…
Ryanqy
  • 8,476
  • 4
  • 17
  • 27
2
votes
3 answers

Non volatile double checked locking, is it possible?

Here is my singleton class. Static instance field is not volatile thus reordering/visibility problem arises. To solve it instance val field is made final. Since instance is properly constructed its clients should always see val field initialized if…
2
votes
1 answer

Triple checked locking?

So in the meanwhile we know that double-checked-locking as is does not work in C++, at least not in a portable manner. I just realised I have a fragile implementation in a lazy-quadtree that I use for a terrain ray tracer. So I tried to find a way…
Sebastian Mach
  • 38,570
  • 8
  • 95
  • 130
2
votes
2 answers

Java double-checked locking solution?

This is a followup from Java double checked locking. The following code snippet has 2 interesting characteristics. 1) It requires a call to a separate init() method before the object is ready for use. So volatile doesn't help (I know, why don't I…
Rich
  • 658
  • 1
  • 9
  • 18
2
votes
2 answers

How JIT schedule the execution of byte code?

Say, We have the code: Test t = new Test(); Compile into byte code it would actually be three steps : 1. mem = allocateMem() : allocate memory for Test and save it's address. 2. construct(mem) : construct the class Test 3. t = mem : point t to the…
WoooHaaaa
  • 19,732
  • 32
  • 90
  • 138
2
votes
2 answers

Does this redeem a thread-safe Double-Checked Locking pattern?

The problems with the original Double-Checked Locking pattern have been well-documented: C++ and the Perils of Double-Checked Locking. I have seen the topic come up in questions on SO fairly often. I have come up with a version that, to me, seems to…
Lubo Antonov
  • 2,301
  • 14
  • 18
2
votes
4 answers

Java synchronized effect on Double-checked locking?

I have read through different articles such as Double-checked locking: Clever, but broken and I understand the reason of why the following code is broken in multi-threading usage. class SomeClass { private Resource resource = null; public…