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

C++ and the Perils of Double-Checked Locking : Workaround?

I was reading a paper on "Perils of double check locking by Scott Meyers". http://www.aristeia.com/Papers/DDJ_Jul_Aug_2004_revised.pdf The author gives reasons why the double check lock fails (page 3, section 4). I was thinking of ways to get around…
Ram
  • 3,045
  • 3
  • 27
  • 42
0
votes
2 answers

update memory before synchronization?

it is mentioned in the Java Memory Model that: When a thread exits a synchronized block as part of releasing the associated monitor, the JMM requires that the local processor cache be flushed to main memory. Similarly, as part of acquiring the…
0
votes
0 answers

How to demonstrate Java DCL problems?

In Java Double checked locking (DCL) attempts to improve performance of lazy instantiation but without memory barriers is exposed to dangerous race conditions between the setting of a variable's address and the publishing of its value to main…
Gonen I
  • 5,576
  • 1
  • 29
  • 60
0
votes
3 answers

How is this code snippet an example of incorrect synchronization?

I am trying to understand the example with incorrect sync code from The Go Memory Model. Double-checked locking is an attempt to avoid the overhead of synchronization. For example, the twoprint program might be incorrectly written as: var a…
0
votes
1 answer

Java double checked locking code explanation

I have a code snippet that I don't understand I will highlight the bit that confused me. private static final Object lock = new Object(); private static volatile YourObject instance; public static YourObject getInstance() { YourObject r =…
Frank Dax
  • 108
  • 7
0
votes
2 answers

LazyReference with double-checked locking and null handling

I've been using LazyReference class for a few years (not on a regular basis of course, but sometimes it is very useful). The class can be seen here. Credits go to Robbie Vanbrabant (class author) and Joshua Bloch with his famous "Effective Java 2nd…
Volo
  • 28,673
  • 12
  • 97
  • 125
0
votes
3 answers

Double checked locking implementation - using atomic vs not using

I saw this cpp core guideline and I'm trying to understand something: http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#cp111-use-a-conventional-pattern-if-you-really-need-double-checked-locking Why is the first example marked as bad? Is…
user2717954
  • 1,822
  • 2
  • 17
  • 28
0
votes
1 answer

How to remove double checking when setting a value in a multithreaded environment

I have a class Checker that stores the field archiveDate.There are several threads that can set this field. They only record the field if it is not yet available. And if one of the threads recorded the date, the other threads don't have to…
Violetta
  • 509
  • 4
  • 12
0
votes
1 answer

Difference in Implementations of Thread Safe Singleton Design Patteren

What is the difference between thread-safe Singleton Design Pattern with Double Check Locking as in the below code. public class Singleton { private static volatile Singleton instance; private Singleton() {} public static Singleton…
0
votes
0 answers

Double-checked locking not working - why?

We are working in an OSGi context and are getting instances of our services via this idiom which includes double-checked locking: private volatile ServiceTracker serviceTrackerField = null; public IMyService…
0
votes
3 answers

Does "Double Checked Locking" work in ColdFusion?

I have used a version of double checked locking in my CF app (before I knew what double checked locking was). Essentially, I check for the existance of an object. If it is not present, I lock (usually using a named lock) and before I try and create…
Jeremy French
  • 11,707
  • 6
  • 46
  • 71
0
votes
2 answers

C++ singleton implementation, double-checked locking

I heard and read a lot about singleton implementation approaches in C++, like Meyer, Phoenix, etc., but all of them seemed to have a problem in certain usage scenarios. So I came up with my own implementation approach, the Daniel Singleton. What I…
0
votes
2 answers

Double-checked locking of non-null class member field

I'm aware of the classic double-checked locking idiom for Java, which first checks if a given field is null and, if so, acquires a lock on the class which has the field: // Double-check idiom for lazy initialization of instance fields // See Pascal…
0
votes
2 answers

Double-Check-Locking guarantees state of the object ? (concurrency in practice)

I am reading concurrency in practice and have some misunderstanding. quote: the real problem with DCL is the assumption that the worst thing that can happen when reading the shared object reference without synchronization is to erroneously see…
gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
0
votes
0 answers

Is it possible Compiler reorder code into critical_section?

I'm new! I have read "C++ and The Perils of Double-Checked Locking" Is it possible Compiler reorder code into critical_section? I guess in that article "Lock" is critical_section. but I have known reordering couldn't happen into…