views:

47

answers:

1

I am developing a class, which can be instantiated before main() is called. I have a critical sections protected by a mutex is the code. Unfortunately, the application fails on AIX as the code is called before threads are initialized. I want to add a check to the code to avoid mutex locking if threads are not ready and use the locking after threads are initialized.

I work on AIX 5.3 with IBM XL C/C++ v.8.0

A: 

You can use a global bool like this:

bool main_called = false;

class AClass {
   AClass()
   {
       if (main_called) mutex_aquire();
       ...
       if (main_called) mutex_release();
   }
};

int main()
{
    main_called = true;
    ...
}

However your description of the implementation shows signs of bad designing. Try to avoid global variables, or at least try to initialize them after main is called:

AClass *aclass;

int main()
{
    aclass = new AClass();
}
Alexandru
I cannot implement your solution as I do not have access to main(). I need a library call that can tell me whether thread initialization is finished.The problem is how to get that information, not where to store it.
Michael