How to make piece of code be the very first to run...
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?
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.
Also, you cold just use the CreateGUID functions provided by windows (if that is your target OS) to create the 'random' strings.
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.
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?
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.
Most implementations of the singleton pattern do this sort of thing.
Obviously, what you need is a COMEFROM keyword. Then you could just go
COMEFROM Start_of_program
//initialise...
RETURN
Simple!
COMEFROM Start_of_program
//initialise...
RETURN
Simple!
is this inter or intraprocess? why couldnt you just register you're mutex handles before initializing you're threads?
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.
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.
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.
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:
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.
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; }}// headerclass 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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement