News for hackers. Stuff that matters.
Re:Some things every C programmer should know about C (2002) by zabzonk on Monday December 09, 2019 @07:18PM (#21748264)
Static variable initialisation that is not initialisation of such variables local to functions is performed before the program starts running, so there cannot be multiple threads.

I can't remember the case in C, but in C++ the initialisation of a function local static variable is guaranteed to be thread-safe.

Re:Some things every C programmer should know about C (2002) by int_19h on Monday December 09, 2019 @07:37PM (#21748413)
It doesn't matter that much whether the variable is global or local, but rather what the valid initializers are.

In C, all statics can only be initialized with compile-time constants. Thus, they can all be initialized before anything in the program starts running (indeed, there's no code initializing them in most implementations - they are just bytes in the data segment).

In C++, initializers can be arbitrary expressions, which means that an initializer for a global variable can spawn a thread, and that thread might still be running when another global is being initialized with a different expression that is not a compile-time constant. There are no thread safety guarantees wrt those kinds of conflicts, for either globals or locals. But for static locals with runtime initializers, the evaluation of initializer is deferred until execution actually reaches the definition of that variable - and thus C++ has a special provision for thread-safe synchronization if that happens on two different threads concurrently.

Re:Some things every C programmer should know about C (2002) by jcelerier on Monday December 09, 2019 @09:21PM (#21749018)
> Static variable initialisation that is not initialisation of such variables local to functions is performed before the program starts running, so there cannot be multiple threads.

you forget the dlopen case :)

Re:Some things every C programmer should know about C (2002) by yuushi on Tuesday December 10, 2019 @01:58AM (#21750303)
> I can't remember the case in C, but in C++ the initialisation of a function local static variable is guaranteed to be thread-safe.

Only in C++11 onwards.