I have a trivial singleton class. My singleton.h file looks something like this:
class singleton { ... public: static singleton& instance(); };
And my singleton.cpp looks like this:
... singleton& singleton::instance() { static singleton * const _instance(new singleton); return *_instance; }
When writing this class, I thought I was relying on a thread-safe function-local static initialization, which, as I understand it, is outlined in section 6.7 of the C ++ standard, as described here . I hope I understand how this should work.
I have been running Visual C ++ since November 2013 CTP toolchain. Microsoft claims that November 2013, CTP supports thread-safe local static initialization, and a quick look at the object code generated by the compiler shows that it is trying to do this.
My problem is that to destroy an object of static storage duration in another translation unit, access to singleton::instance() is required. I expected this to not cause any difficulties, because the static support of singleton::instance() variables is a pointer that is never deleted. However, calls to singleton::instance() from this other object break my process, and the stack trace looks like this:
_Init_thread_header singleton::instance other_translation_unit_object::~other_translation_unit_object
Where _Init_thread_header() seems to be inserted by the compiler to implement stream initialization, static initialization.
So my question is: does the above code show that I fundamentally misunderstand how static initialization should work (most likely a case, so please, if so :), or maybe something else is wrong?
c ++ multithreading c ++ 11 visual-c ++ singleton
0xbe5077ed
source share