...

/

Thread-Safe Initialization of Data

Thread-Safe Initialization of Data

In this lesson, we will learn about the three ways for thread-safe initialization of data.

If the variable is never modified there is no need for synchronization by using an expensive lock or an atomic. We must ensure that it is initialized in a thread-safe way.

There are four ways in C++ to initialize variables in a thread-safe way.

  • Constant expressions.
  • The function std::call_once in combination with the flag std::once_flag.
  • A static variable with block scope.

🔑 Thread-safe initialization in the main-thread

The easiest and fourth way to initialize a variable in a ...