Is __attribute __ ((constructor)) guaranteed to be called exactly once?
Are the constructors and destructors of the GCC shared library defined with __attribute__((constructor))
and __attribute__((destructor))
guaranteed to run exactly once? The documentation seems to imply that they will run at least once, but they don't mention anything more than once.
In other words, if I am doing an operation in the constructor that needs to be done only once, I need to protect it like this:
static gboolean constructor_has_run = FALSE;
if(!constructor_has_run) {
do_operation();
constructor_has_run = TRUE;
}
source to share
If you use it __attribute__((constructor))
, it will be called at the start of execution.
This way, you don't need to protect as you mentioned above.
If you mentioned too it is not.
For more information on __attribute__((constructor))
you can look at https://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
source to share