Say I have a DLL that has the following static/global:
ClassA Object;
Along with the implementation of ClassA, it also contains a ‘regular’ ClassB, which will not work properly if ClassA has not been constructed yet (which is why I’ve made ClassA is a static/global).
In Windows, I believe that the DLL loader will load this DLL on the call to ClassB’s constructor, correct? At this point, ClassA will be constructed and then ClassB’s construction will follow. If a second thread comes along and constructs ClassB, ClassA will not be constructed as it has already been constructed.
Now, my question is — what if ClassB is constructed simultaneously by two threads. So Thread 1 will start to construct ClassA. Will Thread 2 wait until ClassA is completely constructed before executing ClassB’s constructor?
In other words, does LoadLibrary() use a CriticalSection to ensure thread-safe initialization of a DLL’s static/globals? My hunch is ‘yes’, but I can’t seem to find any documentation saying one way or the other.
DllMainis called by the Windows loader while holding an internal critical section known as the “loader lock,” so your static constructors will be called during theDLL_PROCESS_ATTACHevent, which only occurs once, when your DLL is first loaded.