How to determine/influence c++ static initialization order?

How to determine/influence c++ static initialization order?

Post by Michael Dalpe » Sun, 22 Nov 1998 04:00:00



Hi,

I am using Sun C++ 4.2 under Solaris 2.6.  I have a program that is
deadlocking on a mutex during static initialization.  The stack at the point
of deadlock does not make any sense to me.  The code that is deadlocking
looks something like this:

void
SomeRoutine()
{
    pthread_mutex_lock();

    SomeOtherRoutine();

    pthread_mutex_unlock();

Quote:}

The stack looks something like:

STATIC_CONSTRUCTOR
SomeRoutine()
SomeOtherRoutine()
STATIC_CONSTRUCTOR
SomeRoutine()

Because the first SomeRoutine()  locked the mutex, the second SomeRoutine()
deadlocks.

What I don't understand is this: since SomeOtherRoutine() does not call any
other functions (it just does a simple computation), why is the second
STATIC_CONSTRUCTOR occuring before the first SomeRoutine() returns?  Is
there some way to get the compiler/linker to print the static initialization
order or code?  Does this seem like reasonable behavior?  Any information
you can provide or point me to on the mechanics of static initialization
would be greatly appreciated.

Thanks,

Mike