News for hackers. Stuff that matters.
Re:Singleton Pattern in Go by Manishearth on Sunday July 12, 2015 @02:41PM (#9874423)
On C++ if we're using a bool as the "check" and pthread_mutex_lock on a regular mutex would that work, or do we still need to hardcode fences?

(Asking because this pattern is used by libcxxabi code)

Re:Singleton Pattern in Go by maxlybbert on Monday July 13, 2015 @08:52PM (#9882199)
A few years ago, the C++ standard didn't guarantee that the double-checked idiom would work. Even if your threading library had some way to insert the necessary memory barriers, it was possible that your compiler would optimize important things away.

C++11 changes that. The language now has standard ways to add memory barriers, and compiler optimizations can't remove them.

Re:Singleton Pattern in Go by millstone on Sunday July 12, 2015 @03:38PM (#9874620)
Yes, this is broken on certain architectures like PPC. Another core may see the "check" bool as set, but the fields of the protected object as uninitialized. One way to address this is to insert a write barrier just before setting the check bool, and a read barrier just after reading it.