Could double-checked locking accidentally happen to work?

Raf Schietekat
Total Points:
16,805
Status Points:
16,805
Black Belt
July 7, 2009 10:16 AM PDT
Rate
 
#2 Reply to #1

"However, I would strongly recommend against relying on this, as the smallest change could affect the behaviour so that it is no longer what you may expect."
You're preaching to the choir. :-)

"In particular, the compiler may choose to generate the write to pInstance before it invokes the constructor of the new Singleton object"
Even with optimisation turned off? Remember, I'm trying to crack through "if it's not broken, don't fix it", so I'll have to be convincing.

"If you are going to rely on compiler and platform specific behaviour, then you are better off using the compiler-specific atomic primitives such as the gcc __sync_xxx builtins. Such primitives will have a documented consequence on the compiler behaviour, and might therefore provide the requisite guarantees."
Seems like overkill: they're documented as "full barrier", which is too expensive (I only need release-store and load-acquire, which shouldn't take more than a single MOV of properly aligned data).



Intel Software Network Forums Statistics

8472 users have contributed to 31603 threads and 100653 posts to date.
In the past 24 hours, we have 31 new thread(s) 112 new posts(s), and 166 new user(s).

In the past 3 days, the most popular thread for everyone has been gemm(A,A,A) like possible? The most posts were made to gemm(A,A,A) like possible? The post with the most views is Dear Steve, excuse me for a d

Please welcome our newest member Edwin B. Ramayya