Since definitions of behaviour of your singleton are located within its library, it means that singleton instance will be unique and it will exist within the compilation unit where it was created.
Let's say that in libdodo
there is Dodo.cpp
, where you have:
static Dodo& Dodo::instance()
{
static Dodo dodo;
return dodo;
}
Note that local static variables are initialized the first time execution reaches their declaration, so in this case when Dodo::instance
is called first time, so most likely you won't have any problems with lazy initialization of singleton like this.
The only fact that plays role here is thread safety, since there is a possible race condition when more threads call Dodo::instance()
for the first time. For more information I recommend you to read:
this question: Singleton & Multi-threading
this article: C++ scoped static initialization is not thread-safe, on purpose!
and this question could help you as well: Thread safe lazy construction of a singleton in C++
Also note that in C++11 (§6.7.4), initialization of static variables is guaranteed to be threadsafe:
If control enters the declaration concurrently while the variable is being initialized, the concurrent execution shall wait for completion of the initialization.
which means that lazy initialization like this becomes kinda bulletproof ;)