2

In short, I would like a particular piece of static initialization to occur as close to the beginning of main()/DllMain() as possible. It would probably be acceptable for this to be a constructor called last during static initialization.

Since this will almost certainly be a compiler-specific implementation, I'm specifically looking to do this using the visual C++ compiler (VS 2010 and beyond). In the future I will probably need to do this in GCC and Clang, but that's not an immediate concern.

The long story is I have a object in a base library that is statically initialized and creates a thread in its constructor. This thread relies on other statically initialized objects in libraries that we don't control so it causes a race condition in static initialization. If I can pause or avoid creating the thread until all other static initialization is complete this should fix the problem (a simple Sleep(5000) avoids the issue, though that's not exactly a robust solution).

I could explicitly call an initialize function in our main() function, however this isn't ideal as we have 20+ binaries that use this library so every programmer would have to remember to run the initialization in every binary. I would prefer to push this responsibility to the compiler if possible.

Screndib
  • 1,024
  • 11
  • 23
  • What about lazy initialization at the first point one of your libary functions is used? – PlasmaHH Feb 08 '13 at 16:39
  • Now, this is just my 2-cents, but I believe if you WANT to use a library, you'd do WELL to initialize it. Manually. Other than that, how about having a global object of that particular class? That way, it's constructor will be called before main. Though I'll say it again: In my opinion the programmers should initialize it manually. That way, you're 100 % sure the order is maintained. – Refugnic Eternium Feb 08 '13 at 16:39
  • I don't get why you cannot simply call this function in main anway, as you'll inevitably have to touch all the files which contains the main to put this "hack" in.. – Nim Feb 08 '13 at 16:41
  • RefugnicEternium: I agree for most cases, however this is a performance tracking library that the programmer creating the executable may never directly interact with (e.g. it's used by another library that they're using). There is already a global object, but since it starts a thread it's running into static init order race condition with a 3rd party library (boost asio). Nim: I'm looking for a solution that can be entirely implemented within the library code. PlasmaHH: There isn't a function that's called to hook the init routine. – Screndib Feb 08 '13 at 17:22

2 Answers2

2

The comment of Refugnic Eternium above is correct, and offers one solution.

The best solution is to have a function like this:

BOOL InitMyLib();

All functions in MyLib.dll, and all constructors, should fail until this has been called. That way you can ensure that programmers don't forget to call it.

Ben
  • 34,935
  • 6
  • 74
  • 113
0

Create an initializer class and then declare a static instance of it:

class MyInitializer
{
    public:
    MyInitializer ()
    {
        doInitStuffHere();
    }
};

static MyInitializer myInit;

You can control when the static variable is initialized by using #pragma init_seg. For example, #pragma init_seg(lib).

Use #pragma init_seg to Control Static Construction (KB104248)

Jari Karppanen
  • 406
  • 3
  • 6
  • I would want to put this in the init_seg(user), however I don't see how I can say "initialize this last after all of the other init_seg(user) objects" without resorting to command line link ordering. – Screndib Feb 08 '13 at 20:11