Hi all,
I was reading this article about "Double-Checked locking" and out of the main topic of the article I was wondering why at some point of the article the author uses the next Idiom:
Listing 7. Attempting to solve the out-of-order write problem
public static Singleton getInstance()
{
if (instance == null)
{
...
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 == null) { // First check (no locking)
synchronized(this) {
result = f...
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 doesn't exist. It runs in a multi-threaded environment and I want to avoid the primary-key...
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 exsistance of an object. If it is not present I lock (usually using a named lock) and before I try and create the object I check for exsistance again. I thought this was a neet way to stop multiple obj...
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 singleton {
private:
singleton(); // private constructor so users must call instance()
...
Hi All,
I happened upon an article recently discussing the double checked locking pattern in Java and it's 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 articles on the subject and understand the potential issues with getting a re...
I came accross this on the Mike Ash "Care and feeding of singletons" and was a little puzzeled by his comment:
This code is kind of slow, though.
Taking a lock is somewhat expensive.
Making it more painful is the fact
that the vast majority of the time,
the lock is pointless. The lock is
only needed when foo is nil, which
...
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<string, object> _cache =
new Dictionary<string, object>();
public static object Create(string key)
{
object val;
if (!_cac...
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 determine if my application is streaming data or not. I have 3 methods, startStreaming, ...
Anything wrong with the following Singleton implementation?
Foo& Instance() {
if (foo) {
return *foo;
}
else {
scoped_lock lock(mutex);
if (foo) {
return *foo;
}
else {
// Don't do foo = new Foo;
// because that line *may* be a 2-step
/...