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

Singleton pattern and broken double checked locking in a real-world Java application

I was reading the article Double-checked locking and the Singleton pattern, on how double checked locking is broken, and some related questions here on Stack Overflow. I have used this pattern/idiom several times without any issues. Since I have…
saugata
  • 2,823
  • 1
  • 27
  • 39
6
votes
2 answers

Singleton double-check concurrency issue

The fallowing clause is taken from jetbrains.net After reading this and some other articles on the web, I still don't understand how is it possible to return null, after the first thread go in to the lock. Some one that does understand it can please…
choppy
  • 739
  • 1
  • 12
  • 22
5
votes
3 answers

What's wrong with this fix for double checked locking?

So I've seen a lot of articles now claiming that on C++ double checked locking, commonly used to prevent multiple threads from trying to initialize a lazily created singleton, is broken. Normal double checked locking code reads like this: class…
5
votes
2 answers

Explain race condition in double checked locking

void undefined_behaviour_with_double_checked_locking() { if(!resource_ptr) #1 { std::lock_guard lk(resource_mutex); #2 if(!resource_ptr) #3 …
q0987
  • 34,938
  • 69
  • 242
  • 387
5
votes
2 answers

Again double-checked locking and C#

Recently I have been refactoring some of my C# code and I found a few double-checked locking practices taking place. I didn't know it was a bad practice back then and I really want to get rid of it. The problem is that I have a class that should be…
Ivaylo Slavov
  • 8,839
  • 12
  • 65
  • 108
5
votes
5 answers

Does this code solve the double checked locking issue in Java?

Does this code solve the double checked locking issue in Java? public class DBAccessService() { private static DBAccessService INSTANCE; private DBAccessService() {} public static DBAccessService getInstance() { if (INSTANCE…
Aliaksandr Kazlou
  • 3,221
  • 6
  • 28
  • 34
5
votes
1 answer

Double check lock optimization to implement thread-safe lazy-loading in Swift

I have implemented what I think is a double check locking in a class to achieve thread safe lazy loading. Just in case you wondered, this is for a DI library I'm currently working on. The code I'm talking about is the following: final class…
5
votes
2 answers

How should "Double-Checked Locking" be implemented in Delphi?

In C#, the following code (from this page) can be used to lazily instantiate a singleton class in a thread safe way: class Foo { private volatile Helper helper = null; public Helper getHelper() { if (helper == null) { …
mjn
  • 36,362
  • 28
  • 176
  • 378
5
votes
1 answer

Do I need to synchronize ConcurrentMap when adding key only if needed?

I have a ConcurrentMap object. I want to write a method that would return the SomeObject value if it exists, or create a new SomeObject, put it in the Map, and return it if it doesn't exist. Ideally, I could use ConcurrentMap's…
isapir
  • 21,295
  • 13
  • 115
  • 116
5
votes
4 answers

Is this broken double checked locking?

Checkstyle reports this code as "The double-checked locking idiom is broken", but I don't think that my code actually is affected by the problems with double-checked locking. The code is supposed to create a row in a database if a row with that id…
5
votes
5 answers

Resetting a field lazy-loaded with the double-check idiom

Consider the "double-check idiom for lazy initialization of instance fields": // Item 71 in Effective Java copied from this interview with Bloch. private volatile FieldType field; FieldType getField() { FieldType result = field; if (result…
les2
  • 14,093
  • 16
  • 59
  • 76
5
votes
2 answers

double checked locking - objective c

I realised double checked locking is flawed in java due to the memory model, but that is usually associated with the singleton pattern and optimizing the creation of the singleton. What about under this case in objective-c: I have a boolean flag to…
bandejapaisa
  • 173
  • 1
  • 5
5
votes
1 answer

Is `volatile` required for double-checked locking in Java but not C#?

It's very well known among Java programmers that in order for double-checked locking to function correctly, the variable must be declared volatile, and synchronizing the initialization of the object is not enough. The awareness is probably mostly…
Theodore Murdock
  • 1,538
  • 1
  • 13
  • 28
5
votes
1 answer

Lazy initialization for free

In an article on the double-checked locking idiom, I found this quote: One special case of lazy initialization that does work as expected without synchronization is the static singleton. When the initialized object is a static field of a class with…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
4
votes
1 answer

Is implementation of double checked singleton thread-safe?

I know that the common implementation of thread-safe singleton looks like this: Singleton* Singleton::instance() { if (pInstance == 0) { Lock lock; if (pInstance == 0) { Singleton* temp = new Singleton; // initialize to temp …
Denis
  • 2,786
  • 1
  • 14
  • 29
1 2
3
9 10