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
0
votes
6 answers

Double checked locking in C#

The idea is to get a value ("data") from a variable if doesn't exist, then from a file if doesn't exist, then from a server public static string LoadData(int id) { if (_isDataLoaded[id - 1]) { return _data[id - 1]; } if…
Alexandre
  • 13,030
  • 35
  • 114
  • 173
-1
votes
2 answers

double check lock without volatile is wrong?

i use jdk1.8. i think that double check lock without volatile is right. I use countdownlatch test many times and the object is singleton. How to prove that it must need “volatile”? update 1 Sorry, my code is not formatted, because I can’t receive…
-1
votes
1 answer

Double checked locking for static fields in java

I am trying to fix double checked locking with Bloch's effective java recommendation. But a small variation in my code is that the field type is static and the method to create the field type is instance method. Will the below variation for creating…
-1
votes
1 answer

Why use both local variable and volatile both togather for DCL (as suggested by Sonar tool)?

There are related questions but they as the focus in those is more on volatile and not much on the usage of local variable, hence I am posting a new question, Please see the question below. Sonar Source rule has a code according to which below code…
nits.kk
  • 5,204
  • 4
  • 33
  • 55
-1
votes
3 answers

Convert double check locking from using synchronized to locks in JAVA

Consider the following code implementing double check locking using the synchronized keyword in JAVA 8: private static void redoHeavyInitialisation() { if (needToReinitialise()) { synchronized (MyClass.class) { if…
-1
votes
1 answer

Replace double checked locking in concurrent environment

I have already topic with same code: public abstract class Digest { private Map cache = new HashMap<>(); public byte[] digest(String input) { byte[] result = cache.get(input); if (result == null) { …
gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
-2
votes
1 answer

Maybe (... yeah right) solved: Double checked locking on C++: new to a temp pointer, then assign it to instance

This is a follow up of this post on double checked locking. I am writing a new post because it seems that posting a follow-up on "aged" posts doesn't make it as visible/active as sending out a new post, maybe because most people do not sort posts in…
moog
  • 383
  • 5
  • 13
-4
votes
2 answers

Java Double checked locking - initialization method

Consider the example: class SomeClass{ private Foo val; String getVal(){ if(val == null){ synchronized(this){ if(val ==null) val = generateFoo(); …
Bober02
  • 15,034
  • 31
  • 92
  • 178
1 2 3
9
10