Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the C++ standard require that dynamic initialization of static variables be performed in the main thread?

Does the C++ standard require that dynamic initialization of non-local static variables, be performed in the same thread that calls main()?

More specifically, in C++11, is std::this_thread::get_id() guaranteed to return the same result in static initializers and inside main()?

Edit:

Even more specifically, given the following code:

#include <iostream>
#include <thread>

static std::thread::id id = std::this_thread::get_id();

int main()
{
        std::cout << id << "\n";
        std::cout << std::this_thread::get_id() << "\n";
        return 0;
}

are the two emitted thread IDs required/guaranteed to match?

like image 571
Jeremy Avatar asked Apr 21 '15 14:04

Jeremy


People also ask

Do static variables need to be initialized in C?

Static variables are initialized only once. Compiler persist the variable till the end of the program. Static variable can be defined inside or outside the function. They are local to the block.

What is static and dynamic initialization in C?

Static Initialization: Here, the variable is assigned a value in advance. This variable then acts as a constant. Dynamic Initialization: Here, the variable is assigned a value at the run time. The value of this variable can be altered every time the program is being run.

What is dynamic initialization of variables in C?

Dynamic initialization of object refers to initializing the objects at run time i.e. the initial value of an object is to be provided during run time. Dynamic initialization can be achieved using constructors and passing parameters values to the constructors.

Where should static variables be initialized?

As static variables are initialized only once and are shared by all objects of a class, the static variables are never initialized by a constructor. Instead, the static variable should be explicitly initialized outside the class only once using the scope resolution operator (::).


1 Answers

No. The standard nowhere provides such a guarantee, and in fact the contrary is implied by [basic.start.init]/p2:

If a program starts a thread (30.3), the subsequent initialization of a variable is unsequenced with respect to the initialization of a variable defined in a different translation unit. Otherwise, the initialization of a variable is indeterminately sequenced with respect to the initialization of a variable defined in a different translation unit. If a program starts a thread, the subsequent unordered initialization of a variable is unsequenced with respect to every other dynamic initialization. Otherwise, the unordered initialization of a variable is indeterminately sequenced with respect to every other dynamic initialization.

There would be no need to weaken the sequencing guarantee in the presence of threads if all initializations had to be performed on the same thread.

like image 54
T.C. Avatar answered Sep 18 '22 18:09

T.C.