Is there a way I can identify if a function is being called the for the first time by identifying the internal (hidden?) variables that GCC uses to facilitate static variables (variables that already exist in my function)?
I hope to get at these variables from the C++ code.
GCC uses a hidden flag indicating if a variable has been initialized. There is no way to access this. Even so, these don’t actually track the “first time” but rather whether the variable has been initialized. Consider the following:
But the standard would actually allow that these are initialized by different threads. So if that happens which one got their first. Checking the gcc disassembly it does seem each is handled with its own lock, at least in non-optimized mode, the code changes a lot in optimized mode (so even moreso, “first time” is not clearly defined).
Additionally, as Mark points out in his comment, it greatly depends on what type you are using when the initialization is done. Simple types might, not guaranteed, be initialized globally, others will indeed wait until the function is called first.
Why do you need to know first time entry anyway?