c++ - How can I call a function or statically initialize an object immediately before main? -
in short, particular piece of static initialization occur close origin of main()/dllmain() possible. acceptable constructor called lastly during static initialization.
since compiler-specific implementation, i'm looking using visual c++ compiler (vs 2010 , beyond). in future need in gcc , clang, that's not immediate concern.
the long story have object in base of operations library statically initialized , creates thread in constructor. thread relies on other statically initialized objects in libraries don't command causes race status in static initialization. if can pause or avoid creating thread until other static initialization finish should prepare problem (a simple sleep(5000) avoids issue, though that's not robust solution).
i explicitly phone call initialize function in our main() function, isn't ideal have 20+ binaries utilize library every programmer have remember run initialization in every binary. prefer force responsibility compiler if possible.
the comment of refugnic eternium above correct, , offers 1 solution.
the best solution have function this:
bool initmylib();
all functions in mylib.dll, , all constructors, should fail until has been called. way can ensure programmers don't forget phone call it.
c++ visual-c++ main static-initialization
No comments:
Post a Comment