LoadLibrary Static/Globals and Threads

2019-07-19 18:33发布

问题:

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.

回答1:

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.



回答2:

Look at the documentation for DllMain; I believe it talks about the loader lock and initialization order.



回答3:

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.