LoadLibrary Static/Globals and Threads

纵饮孤独 提交于 2019-12-13 16:07:22

问题


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.



来源:https://stackoverflow.com/questions/1875552/loadlibrary-static-globals-and-threads

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!