
On 15/05/11 04:52, John Maddock wrote:
Folks,
I'm looking at the code for singleton_pool, and I can't *quite* convince myself that it's completely thread safe and guaranteed to do what it says, the code in question is in boost/pool/detail/singleton.hpp which is a deceptively clever/simple singleton implementation. I haven't been able to break it in practice, but would a sufficiently clever compiler be able to optimize away the call create_object.do_nothing(); and therefore not instantiate the object prior to main()?
No cause it's static and hence compiler wont remove it. I think it is suffisent, but I would worry of *other* static object in different TU calling singleton instance as the order of static initializer is random. Thread safe singleton are huge beasts to get right, see http://stackoverflow.com/questions/6915/thread-safe-lazy-contruction-of-a-si... I think our best bet is having some atomic compare&swap in the singleton construction to be sure it is done properly.