double-checked-locking

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 timesSafelyGotten = 0; private Helper helper = null; public getHelper() { if (timesS...

Triple checked locking?

So in the meanwhile we know that double-checked-locking as is does not work in C++, at least not in a portable manner. I just realised I have a fragile implementation in a lazy-quadtree that I use for a terrain ray tracer. So I tried to find a way to still use lazy initialization in a safe manner, as I wouldn't like to quadruple memory ...

Singleton pattern and broken double checked locking in 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 stackoverflow. I have used this pattern/idiom several times without any issues. Since I have been using Java 5, my first thought was that this has been rectified in Java 5 memory model....

Threadsafe lazy loading when the loading could fail

Hello, I've been spending about an hour searching for a concensus on something I'm trying to accomplish, but have yet to find anything conclusive in a particular direction. My situation is as follows: I have a multi-threaded application (.NET web service) I have classes that use objects that take non-negligible time to load, so I wou...

Maybe (... yeah right) solved: Double checked locking on C++: new to a temp pointer, then assign it to instance.

Hi all, This is a follow up of this post on double checked locking. I am writing a new post because it seems that posting a follow-up on "aged" posts doesn't make it as visible/active as sending out a new post, maybe because most people do not sort posts in stackoverflow by level of activity. To all who responded, thank you for all you...

Double-checked locking for growable array of binomial coefficients

I'm trying to use double-checked locking to maintain an array of binomial coefficients, but I read recently that double-checked locking doesn't work. Efficiency is extremely important so using volatile isn't an option unless it's only inside the conditional statements. I can't see a way to use a static class with a singleton object (th...

Does Java synchronized keyword flush the cache?

Java 5 and above only. Assume a multiprocessor shared-memory computer (you're probably using one right now). Here is a code for lazy initialization of a singleton: public final class MySingleton { private static MySingleton instance = null; private MySingleton() { } public static MySingleton getInstance() { if (instance == ...

Double checked locking pattern: Broken or not?

Why is the pattern considered broken? It looks fine to me? Any ideas? public static Singleton getInst() { if (instace == null) createInst(); return instace; } private static synchronized createInst() { if (instace == null) { instace = new Singleton(); } } ...

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 have it thread-safe. I know that double-checked-locking in Java is "teh 3vilness!!1", but ...