I’ve just read that if I want to be sure about the initialization order it will be better to use some function which will turn global variable into local(but still static), my question, do I need to keep some identifier which tells me that my static object has already been created(the identifier inside function which prevent me from the intialization of the static object one more time) or not? cause I can use this function with initialization in different places, thanks in advance for any help
I’ve just read that if I want to be sure about the initialization order
Share
As far as the standard is concerned, initialization of a function-scope static variable only happens once:
So there’s no need for
gettheintto check whetherfoohas already been initialized – the value won’t be overwritten with3on the second and subsequent calls.Threads throw a spanner in the works, being outside the scope of the standard. You can check the documentation for your threading implementation, but chances are that the once-onliness of the initialization is not thread-safe in your implementation. That’s what
pthread_onceis for, or equivalent. Alternatively in a multi-threaded program, you could call the function before creating any extra threads.