This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c++/83568] New: thread_local class members not initialized before accessed using operator ::


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83568

            Bug ID: 83568
           Summary: thread_local class members not initialized before
                    accessed using operator ::
           Product: gcc
           Version: 7.1.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: tinrow at gmail dot com
  Target Milestone: ---

thread_local class members are not initialized as expected, if it is accessed
using member access operator (dot) instead of scope resolution operator :: .
The initialization only happens on the first time it is accessed using operator
:: .

reproducible code (gcc 4.8 - 7.1):

#include <iostream>
bool initialized = false;

int init() {
    initialized = true;
    return 1;
}

struct A {
    static thread_local int foo;
};

thread_local int A::foo { init() };

int main() {
    A a;
    std::cout << (initialized ? "ok" : "bug" ) << std::endl;
    std::cout << a.foo << std::endl;
    std::cout << A::foo << std::endl;
    return 0;
}


Output:
bug
0
1

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]