Sign in to follow this  
Prozak

How to make piece of code be the very first to run...

Recommended Posts

I've got various code that depends on mutexes to work, because of inter-thread syncronization. To generate these handles for mutexes and events I create random strings of char[80]. But... the very first piece of code I would like to run would be a randomizer, before these strings get generated, or they'll be exactly alike the last run. These are inside various classes that are global. So, how do I make it so a certain piece of code is absolutely the first to run?

Share this post


Link to post
Share on other sites
You can't do it in a general way without a _LOT_ of effort (and it's not worth it). Make a static boolean variable 'FirstRun' initialized to true inside your 'generate string' function, and if it's true, seed the PRNG and set the variable to fasle.
Also, you cold just use the CreateGUID functions provided by windows (if that is your target OS) to create the 'random' strings.

Share this post


Link to post
Share on other sites
Do you want the code to be the absolutely first code to run, or do you just need it to be run before the dynamic initializations of all your global variables? These are two different problems, and depending on your programming language, the two problems can have very different answers. Assuming C++, the first question is not solvable. Also assuming C++, you can solve the second problem by adding a static or anonymous namespaced class object in the header that defines your string generator that in the constructor calls the initializer for the randomizer if the randomizer has not already been initialized. This is the same as the traditional mechanism used to make sure that the C++ iostream objects (cout, etc.) are initialized prior to use.

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
Do you want the code to be the absolutely first code to run, or do you just need it to be run before the dynamic initializations of all your global variables? These are two different problems, and depending on your programming language, the two problems can have very different answers. Assuming C++, the first question is not solvable. Also assuming C++, you can solve the second problem by adding a static or anonymous namespaced class object in the header that defines your string generator that in the constructor calls the initializer for the randomizer if the randomizer has not already been initialized. This is the same as the traditional mechanism used to make sure that the C++ iostream objects (cout, etc.) are initialized prior to use.


Thank you SiCrane for your reply. The language is Visual Studio's C++, win32. I don't actually need it to be the very first piece of code run, just need it to be run before other code that requires randomization.

Perhaps if I make the first "include" in my main.cpp to a file that does this?

Share this post


Link to post
Share on other sites
C++ makes few guarantees about the order in which global objects get initialized and depending on that sort of thing is very fragile. To rephrase what SiCrane said a bit you need to have your dependent objects initialize the randomizer on-demand rather than just assuming it is always initialized.

Most implementations of the singleton pattern do this sort of thing.

Share this post


Link to post
Share on other sites
I think this will solve your problem:

Put the code you want to run in a class constructor.
Instatiate an instance of the class in a global variable (or any variable of with file scope).
The constructor will be called before main() is entered.

Share this post


Link to post
Share on other sites
Quote:
Original post by Prozak
Perhaps if I make the first "include" in my main.cpp to a file that does this?

No, that would not be sufficient since there is no guarantee that the global objects in main.cpp will be initialized before the global objects in other source files. You need to make sure that the randomizer is initialized before any of your strings are initialized.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
This can be done with a static class that creates a ref counted heap instance when first accessed.

The problem is that C++ does not garentee the order of static creation. So making a class create an instance of the required class when it is first needed/referenced allows dependancies to be created in the right order.

in your case you need the name of a mutex so:



template< typename TArg >
class Static
{
static TArg* pInstance;
static unsinged int uiCount;

public:
Static()
{ Create(); }
~Static()
{ Destroy(); }

void Create( void )
{ if( !(++uiCount) )
--uiCount;
if( !pInstance )
pInstance =new TArg;
}
void Destroy( void )
{ if( uiCount && ~uiCount )
--uiCount;
if( !uiCount )
{ delete pInstance;
pInstance =NULL;
}
}

TArg* operator->()
{ return pInstance;
}
}

// header
class MutexNames;
Static<MutexNames> gMutexNames;

// code
gMutexNames.Create(); <-inc ref count, create internal instance if not already.
...
gMutexNames->GetName(); <- do stuff
...
gMutexNames.Release(); <-dec ref count, delete if needed




The instances need to be reference counted because construction and destruction of the class holding the instance does not actually bound the lifetime of class use.

Share this post


Link to post
Share on other sites
How about instead of using random strings you use:
class
{
...
char *mutex_name = __FILE__ __LINE__;
...
}

Wouldn't that work?

And anyway, you should think over having creating classes in global space. It's not a great thing to do, because if they depend on other global classes, all hell will break loose. Also, as any C programmer will tell you, globals are bad.

Share this post


Link to post
Share on other sites
NOTE: Possibly mildly off-topic.

Just had an idea on how to go about ensuring the order of initialisation of all global objects: put them all in a global object class. There are many obvious limitations to this method though.


class CAllocator {
public:
CAllocator() {}
~CAllocator() {}
void* allocate(int size);
void deallocate(void* data);
};

class CRandom {
public:
CRandom();
~CRandom();

int m_value;
};

class CRandomizer {
public:
CRandomizer() {}
~CRandomizer() {}
int rand();
};

class CGlobals {
public:
CGlobals();
~CGlobals() {}
CRandomizer randomizer; // etc.
CAllocator allocator; // etc...

CRandom random1;
};

// Object contains all previous global objects.
CGlobals g_globals;

int main() {
return 0;
}

CGlobals::CGlobals() : randomizer(), allocator(), random1() {
// Do Nothing.
}

CRandom::CRandom() {
m_value = g_globals.randomizer.rand();
}

int CRandomizer::rand() {
return 42;
}




Ugly, don't work with libraries, not sure what the order of destruction is, but it could help with some designs.

Something to think about: How could the next version of C++ provide for this?

Constructor for the global namespace??

::() : randomizer(), random1() {}



This doesn't get around the issue with specifying order of construction of objects inside libraries.

I like this one: "relies" keyword.

CRandomizer randomizer;
CRandom random1 relies randomizer;


This says that random1 relies on randomizer being constructed first.
Not sure what a good syntax for this would be.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this