• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1.. title:: clang-tidy - bugprone-dynamic-static-initializers
2
3bugprone-dynamic-static-initializers
4====================================
5
6Finds instances of static variables that are dynamically initialized
7in header files.
8
9This can pose problems in certain multithreaded contexts. For example,
10when disabling compiler generated synchronization instructions for
11static variables initialized at runtime (e.g. by ``-fno-threadsafe-statics``), even if a particular project
12takes the necessary precautions to prevent race conditions during
13initialization by providing their own synchronization, header files included from other projects may
14not. Therefore, such a check is helpful for ensuring that disabling
15compiler generated synchronization for static variable initialization will not cause
16problems.
17
18Consider the following code:
19
20.. code-block:: c
21
22  int foo() {
23    static int k = bar();
24    return k;
25  }
26
27When synchronization of static initialization is disabled, if two threads both call `foo` for the first time, there is the possibility that `k` will be double initialized, creating a race condition.
28