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

Lazy initialized caching... how do I make it thread-safe?

that's what I have: a Windows Service C# multithreaded the service uses a Read-Write-Lock (multiple reads at one time, writing blocks other reading/writing threads) a simple, self-written DB C++ small enough to fit into memory big enough not…
Ben
  • 4,486
  • 6
  • 33
  • 48
1
vote
1 answer

How can I create the correct lazy singleton?

I have this class public class Singletons { private static UserService userService; private static OrderService orderService; public static UserService getUserService() { if (userService == null) { synchronized…
Pavel Petrashov
  • 1,073
  • 1
  • 15
  • 34
1
vote
1 answer

Do I have any fault for this java:S3077?

I have a class whose fields can't help but lazily initialized. class Some { public Some getPrevious() { { final Some result = previous; if (result != null) { return result; } …
Jin Kwon
  • 20,295
  • 14
  • 115
  • 184
1
vote
1 answer

LazyArray template via std::unique_ptr, is this a proper implementation of the double-check idiom?

Does this code safely implement the double-check idiom using C++11 atomic? I saw in "The C++ Programing Language 4th ed." an example that uses atomic and I did my best to keep things equivalent but I'm not confident. Also, can this be…
1
vote
1 answer

What the correct way when use Double-Checked Locking with memory barrier in c++?

I just read the excellent blog C++ and the Perils of Double-Checked Locking And I don't understand why we have to use the first memory barrier in Example 12 (as below): Singleton* Singleton::instance () { Singleton* tmp = pInstance; …
ricky
  • 2,058
  • 4
  • 23
  • 49
1
vote
4 answers

Volatile reads clash

Suppose we are instantiating a singleton using double-checked locking: public static Instance getInstance() { if (this.instance == null) { synchronized(Instance.class) { if (this.instance == null) { …
nyarian
  • 4,085
  • 1
  • 19
  • 51
1
vote
2 answers

Is it good to test a condition then lock then re-test the condition

Possible Duplicate: Double-checked locking in .net EDIT: lots of edits to clarify this question is not about singleton I find myself writing code like this: if(resourceOnDiskNeedsUpdating) { lock(lockObject) { …
Myster
  • 17,704
  • 13
  • 64
  • 93
1
vote
1 answer

Sonarqube:Java is not catching "Double-checked Locking" (S2168)

We've started running our code through Fortify, and as an exercise I wanted to see if Sonarqube would pick up any of the same issues. One of the first ones I'm unable to replicate is S2168:Double-Checked Locking The guilty code fragment: if…
Adam L
  • 137
  • 1
  • 10
1
vote
2 answers

Avoiding volatile reads in thread-safe memoizing Supplier

I want to create a memoized version of a given Supplier such that multiple threads can use it concurrently with the guarantee that the original supplier's get() is called at most once, and that all of the threads see that same result. Double-checked…
1
vote
1 answer

What does it mean that singleton DCL broken?

After reading dozens of articles about DCL. I feel that I should not use this concept without volatile.If I will not lead this technique my code will not thread save and very very bad according one hundreed different reasons. Recently I reread…
gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
1
vote
0 answers

Thread safe singleton with deterministic destruction

I want to create a thread safe singleton class. The current way to achieve the same(that I know of) is by having a static method in the class which returns the static object as below: Singleton & Singleton::getInstance() { static Singleton…
Arun
  • 3,138
  • 4
  • 30
  • 41
1
vote
0 answers

Is double-check locking safe in C++ for unidirectional data transfer?

I have inherited an application which I'm trying to improve the performance of and it currently uses mutexes (std::lock_guard) to transfer data from one thread to another. One thread is a low-frequency (slow) one which simply modifies…
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
1
vote
0 answers

Java Threads - Single-Producer and Consumer, using sleep and continue

I was trying to solve the Single Producer & Consumer problem, As per the problem statement from wikipedia https://en.wikipedia.org/wiki/Producer%E2%80%93consumer_problem Production and Consumption should happen endlessly such that the producer…
1
vote
3 answers

Implementation of Double Checked Locking in C++ 98/03 using volatile

Reading this article about Double Checked Locking Pattern in C++, I reached the place (page 10) where the authors demonstrate one of the attempts to implement DCLP "correctly" using volatile variables: class Singleton { public: static volatile…
undermind
  • 1,779
  • 13
  • 33
1
vote
2 answers

Java: How to do double-checked-locking with an array element?

This is what my code currently looks like: private boolean[] isInitialized = new boolean[COUNT]; private void ensureInitialized(int i) { if (! isInitialized[i]) { initialize(i); isInitialized[i] = true; } } Now I want to…
Cephalopod
  • 14,632
  • 7
  • 51
  • 70