Thanks for the reply. No--not really. (* See note at bottom)
What I'm asking is a very general question. Really, it is this:
How do cause some function to be called on main thread initialisation?
I can call it through the use of a dummy variable initialisation, like so:
int dummy = some_func();
But is there a better way? I.e. without having dummy variables every where?
I'm struggling to explain myself without specific examples. So here's another one -- a singleton:
Code:
SomeClass& SomeClass::singleton()
{
static SomeClass sc;
return sc;
}
Now this is OK--except in a multiple threaded application where the singleton() method may be called for the first time by two threads simultaneously, resulting in a race condition during the creation of the static "sc" variable.
I can overcome this by ensuring the singleton() method is initially called in the application's main thread by:
SomeClass& dummy = SomeClass::singleton();
in the cpp file.
However, do I really need to declare a dummy variable in order to get the function to be called on initialisation?
Is there some way in C++ I can get SomeClass::singleton() to be called on start-up without the use of dummy data?
* PS
Thanks for the example Bench82 -- I see what your code is doing now. However, the answer is still no--it's not what I want. I still want a function called on main thread start-up because I need to ensure it is called for the first time in the main thread an not in any other thread. Also, remember, I am writing an api so it is not applicable to call functions in the main() C function.