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
4
votes
2 answers

Double-checked Locking for Shared Pointers

Disclaimer: I come from a Java background, and, as such, I have no clue on how a lot of the internals of C++ (and associated libraries) work. I have read enough to know that double-checked locking is evil, and a correct and safe implementation of a…
afsantos
  • 5,178
  • 4
  • 30
  • 54
4
votes
7 answers

Double checked locking on C++: new to a temp pointer, then assign it to instance

Anything wrong with the following Singleton implementation? Foo& Instance() { if (foo) { return *foo; } else { scoped_lock lock(mutex); if (foo) { return *foo; } else { //…
moog
  • 81
  • 1
  • 5
4
votes
4 answers

Out-of-order writes for Double-checked locking

In the examples mentioned for Out-of-order writes for double-checked locking scenarios (ref: IBM article & Wikipedia Article) I could not understand the simple reason of why Thread1 would come of out synchronized block before the constructor is…
Sandeep Jindal
  • 14,510
  • 18
  • 83
  • 121
4
votes
2 answers

Double-checked locking - pitfalls?

I'm using java for about a month, and still am generally an amateur in programming, so feel free to correct me if I get something wrong. Maybe I'll provide some excess details, but I'm so confused right now that I can't decide what matters…
Timekiller
  • 2,946
  • 2
  • 16
  • 16
3
votes
1 answer

Asynchronous single task executor

I'm in doubt if the solution below is correct for the following: in a multi-user application any user can start processing of given data by clicking a button processing takes long time thus it should be executed asynchronously to not block GUI if…
Bobby_Bob
  • 33
  • 6
3
votes
1 answer

Is double check lock broken for all use case?

I understand that double check locking is broken for singleton lazy initialization: // SingletonType* singleton; // std::mutex mtx; SingletonType* get() { if(singleton == nullptr){ lock_guard _(mtx); if(singleton == nullptr) { …
Yituo
  • 1,458
  • 4
  • 17
  • 26
3
votes
2 answers

Why enum singleton is lazy?

I saw answers like these, tried to clarify via comments, and was unsatisfied by examples here. Maybe it's time for this specific question... Why enum singleton implementation is called lazy? public enum EnumLazySingleton { INSTANCE; …
uvsmtid
  • 4,187
  • 4
  • 38
  • 64
3
votes
3 answers

Double-check locking issues, c++

I left the rest of implementation for simplicity because it is not relevant here. Consider the classical implemetation of Double-check loking descibed in Modern C++ Design. Singleton& Singleton::Instance() { if(!pInstance_) { …
Eduard Rostomyan
  • 7,050
  • 2
  • 37
  • 76
3
votes
2 answers

Double checked locking without using volatile-keyword and without synchronizing the entire getInstance() method

Following is my singleton class where I am using double-checked-locking without using volatile keyword and without synchronizing the entire getInstance() method: public class MySingleton { private static MySingleton mySingleton; public…
3
votes
1 answer

Boost upgrade_lock and DCLP (double-checked locking pattern)

Suppose we have the following (pseudo) code: using UpgradeLock = boost::upgrade_lock; using UpgradeToUniqueLock = boost::upgrade_to_unique_lock; boost::shared_mutex mtx; void DeleteTable() { …
Alexey
  • 1,198
  • 1
  • 15
  • 35
3
votes
2 answers

Fixing DCLP only with volatile

I am reading the article "C++ and the Perils of Double-Checked Locking" which explains the problems in DCLP. The second part of the article (where the link forwards) shows how to try and solve DCLP with merely C/C++ volatile (which from what I know,…
TCS
  • 5,790
  • 5
  • 54
  • 86
3
votes
1 answer

Why does ConcurrentHashMap work in Double Checked Locking

In the book "Java Concurrency in Practice" is mentioned that the following code is not threadsafe: @NotThreadSafe public class DoubleCheckedLocking { private static Resource resource; public static Resource getInstance(){ …
dpolaczanski
  • 386
  • 1
  • 3
  • 18
3
votes
1 answer

Is double-checked locking only broken in a lazy initialization scene?

I read this article: The "Double-Checked Locking is Broken" Declaration, it says Double-Checked Locking is widely cited and used as an efficient method for implementing lazy initialization in a multithreaded environment. Unfortunately, it will…
chanjarster
  • 506
  • 3
  • 17
3
votes
2 answers

Can Java write to the reference to the object before it is constructed by some method?

There was a well-known pitfall while using the double-checked locking pattern (an example and explanation taken from "Concurrency in Practice"): @NotThreadSafe public class DoubleCheckedLocking { private static Resource resource; public static…
MiamiBeach
  • 3,261
  • 6
  • 28
  • 54
3
votes
3 answers

Java double checked locking by forcing synchronization twice, Workable?

I've read all about how double checked locking fixes never work and I don't like lazy initialization, but it would be nice to be able to fix legacy code and such a problem is too enticing not to try to solve. Here is my example: private int…