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
1
vote
1 answer

Threadsafe lazy loading when the loading could fail

I've been spending about an hour searching for a concensus on something I'm trying to accomplish, but have yet to find anything conclusive in a particular direction. My situation is as follows: I have a multi-threaded application (.NET web…
1
vote
1 answer

Why use double checked locking

Regarding a previous question I raised, public static Singleton getInstanceDC() { if (_instance == null) { // Single Checked (1) synchronized (Singleton.class) { if (_instance == null) { // Double…
jayendra bhatt
  • 1,337
  • 2
  • 19
  • 41
1
vote
2 answers

Is double check with final variable working

I have class designed for lazy initialization and storing objects which creation is not necessary threadsafe. Here is the code: class SyncTest { private static final Object NOT_INITIALIZED = new Object(); private Object object; /** …
1
vote
1 answer

Extend a class with double checked locking singleton in Java

I have a class like so: public class Contact { private static volatile Contact instance; private List contacts = new ArrayList<>(); private Context context; public static Contact getInstance(Context context) { …
serg66
  • 1,148
  • 1
  • 17
  • 31
1
vote
1 answer

Is this implementation of Double checked lock pattern (DCLP) in C++11 is correct?

I am reading about DCLP (double-checked lock pattern), and I am not sure I got it right. When using atomics to create the lock (as explained in DCLP fixed in C++11), and there are 2 things that are not clear: In the code from the…
TCS
  • 5,790
  • 5
  • 54
  • 86
1
vote
2 answers

Special case of double checked locking?

So I have this code where lockMap is a ConcurrentHashMap //Creation of locks Lock getLock(String key) { Lock lock = lockMap.get(key); if (lock == null) { synchronized (lockMap) { lock = lockMap.get(key); …
Adrien
  • 55
  • 1
  • 4
1
vote
3 answers

Lazy initialization for non-static values

The question actually refers to a different question, which was closed as duplicate because it was probably not well formulated. What would be an effective alternative lazy initialization idiom instead of double-checked locking for this code sample…
AngryJuice
  • 61
  • 1
  • 6
1
vote
1 answer

Double-checked locking as an anti-pattern

There's a common belief and multiple sources (including wiki) that claim this idiom to be an anti-pattern. What are the arguments against using it in production code given the correct implementation is used (for example, using volatile) What are…
1
vote
2 answers

Map synchronization with double check locking

I've read about double-checked locking and its drawbacks, but I'm asking if using a synchronizedMap can be considered safe. Here is my code: public class EntityUtils { private static final Map> searchMap =…
Michele Mariotti
  • 7,372
  • 5
  • 41
  • 73
1
vote
3 answers

Why doesn't double checked locking fail like this in case of Singleton classes?

1. class Foo { 2. private Helper helper = null; 3. public Helper getHelper() { 4. if (helper == null) { 5. synchronized(this) { 6. if (helper == null) { 7. helper = new…
1
vote
1 answer

double checked locking in JAVA

Reading about DCL in Wikipedia, I was wondering about the problem in DCL and the proposed solution, or in other words why is the volatile keyword needed? The problem, in short: using DCL may result in some cases in a partially constructed object…
1
vote
2 answers

ConcurrentHashMap of Future and double-check locking

Given: A lazy initialized singleton class implemented with double-check locking pattern with all the relevant volatile and synchronized stuff in getInstance. This singleton launches asynchronous operations via an ExecutorService, There are seven…
1
vote
1 answer

Singleton with Double Check Locking - error "being used by another process"

I have been reading a lot of articles about Singleton online, but most of the articles out only demonstrate creating simple objects with properly locking to ensure thread safe without race condition. Simple enough to demonstrate, I put a file…
userb00
  • 589
  • 1
  • 8
  • 16
1
vote
2 answers

Deadlock using double-checked locking in SQL

I'm experiencing deadlocks from my SQL statement in which I want to select an ID if it exists, else insert and then select it. I'm using double checked locking to prevent locking overhead, as suggested here. Obviously I'm doing this to support…
Jeppebm
  • 389
  • 1
  • 3
  • 16
1
vote
2 answers

Double checking for singleton seems to work just fine without getting broken , why and how?

I have read that double checking mechanism for singleton is a failure, because of some memory model followed by JVM which makes the reference read not null even if the constructor has not got executed completely. I tried testing the same by making…
nits.kk
  • 5,204
  • 4
  • 33
  • 55