Reputation: 2996
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.
Upvotes: 2
Views: 2161
Reputation: 11585
DllMain
is called by the Windows loader while holding an internal critical section known as the "loader lock," so your static constructors will be called during the DLL_PROCESS_ATTACH
event, which only occurs once, when your DLL is first loaded.
Upvotes: 1
Reputation: 7493
DLL's are not initialized like EXE's since they are shared by multiple processes. What you need is effectively a singleton object that is a one time factory for your other objects.
Note, I'm assuming here by "ClassA" and "ClassB" you mean instances of those classes...
For example you could have a someting like
ClassA& GetTheClassAInstance();
ClassB& GetTheClassBInstsance();
The first time these are called, these functions would ensure that your global instances of ClassA and ClassB were properly constructed.
Upvotes: 0
Reputation: 11421
Look at the documentation for DllMain; I believe it talks about the loader lock and initialization order.
Upvotes: 1