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
95
votes
8 answers

Why is volatile used in double checked locking

From Head First design patterns book, the singleton pattern with double checked locking has been implemented as below: public class Singleton { private volatile static Singleton instance; private Singleton() {} public static Singleton…
toc777
  • 2,607
  • 2
  • 26
  • 37
62
votes
8 answers

Double-checked locking in .NET

I came across this article discussing why the double-check locking paradigm is broken in Java. Is the paradigm valid for .NET (in particular, C#), if variables are declared volatile?
erikkallen
  • 33,800
  • 13
  • 85
  • 120
53
votes
5 answers

why using volatile with synchronized block?

I saw some examples in java where they do synchronization on a block of code to change some variable while that variable was declared volatile originally .. I saw that in an example of singleton class where they declared the unique instance as…
46
votes
5 answers

What is the point of making the singleton instance volatile while using double lock?

private volatile static Singleton uniqueInstance In a singleton when using double lock method for synchronization why is the single instance declared as volatile ? Can I achieve the same functionality without declaring it as volatile ?
43
votes
11 answers

Java double checked locking

I happened upon an article recently discussing the double checked locking pattern in Java and its pitfalls and now I'm wondering if a variant of that pattern that I've been using for years now is subject to any issues. I've looked at many posts and…
30
votes
5 answers

Double-checked locking without volatile

I read this question about how to do Double-checked locking: // Double-check idiom for lazy initialization of instance fields private volatile FieldType field; FieldType getField() { FieldType result = field; if (result == null) { // First…
21
votes
1 answer

Threadsafe lazy initialization: static vs std::call_once vs double checked locking

For threadsafe lazy initialization, should one prefer a static variable inside a function, std::call_once, or explicit double checked locking? Are there any meaningful differences? All three can be seen in this question. Double-Checked Lock…
Praxeolitic
  • 22,455
  • 16
  • 75
  • 126
20
votes
2 answers

Double checked locking in Android

According to many, the somewhat common Double-Checked Locking idiom is broken for java unless you're running 1.5 or later and use the volatile keyword. A broken double-checked lock sample: // Broken multithreaded version // "Double-Checked Locking"…
emmby
  • 99,783
  • 65
  • 191
  • 249
16
votes
5 answers

Double checked locking on Dictionary "ContainsKey"

My team is currently debating this issue. The code in question is something along the lines of if (!myDictionary.ContainsKey(key)) { lock (_SyncObject) { if (!myDictionary.ContainsKey(key)) { …
16
votes
3 answers

Lazily initialize a Java map in a thread safe manner

I need to lazily initialize a map and its contents. I have the below code till now: class SomeClass { private Map someMap = null; public String getValue(String key) { if (someMap == null) { …
Vivek
  • 2,000
  • 3
  • 16
  • 22
13
votes
5 answers

How to show that the double-checked-lock pattern with Dictionary's TryGetValue is not threadsafe

Recently I've seen some C# projects that use a double-checked-lock pattern on a Dictionary. Something like this: private static readonly object _lock = new object(); private static volatile IDictionary _cache = new…
Amir
  • 4,131
  • 26
  • 36
13
votes
2 answers

C++11: Safe double checked locking for lazy initialization. Possible?

I have read many questions considering thread-safe double checked locking (for singletons or lazy init). In some threads, the answer is that the pattern is entirely broken, others suggest a solution. So my question is: Is there a way to write a…
gexicide
  • 38,535
  • 21
  • 92
  • 152
12
votes
6 answers

Double checked locking with ConcurrentMap

I have a piece of code that can be executed by multiple threads that needs to perform an I/O-bound operation in order to initialize a shared resource that is stored in a ConcurrentMap. I need to make this code thread safe and avoid unnecessary calls…
pmc255
  • 1,499
  • 2
  • 19
  • 31
12
votes
2 answers

double checked locking pattern

In C++ and the Perils of Double-Checked Locking, there's persudo code to implement the pattern correctly which is suggested by the authors. See below, Singleton* Singleton::instance () { Singleton* tmp = pInstance; ... // insert memory…
ashen
  • 807
  • 9
  • 24
11
votes
3 answers

Double-Checked Locking Pattern in C++11?

The new machine model of C++11 allows for multi-processor systems to work reliably, wrt. to reorganization of instructions. As Meyers and Alexandrescu pointed out the "simple" Double-Checked Locking Pattern implementation is not safe in…
towi
  • 21,587
  • 28
  • 106
  • 187
1
2 3
9 10