[llvm-bugs] [Bug 42111] New: thread-local static variable i

via llvm-bugs llvm-bugs at lists.llvm.org
Mon Jun 3 03:40:44 PDT 2019


            Bug ID: 42111
           Summary: thread-local static variable i
           Product: clang
           Version: 8.0
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: C++
          Assignee: unassignedclangbugs at nondot.org
          Reporter: eyalroz at technion.ac.il
                CC: blitzrakete at gmail.com, dgregor at apple.com,
                    erik.pilkington at gmail.com, llvm-bugs at lists.llvm.org,
                    richard-llvm at metafoo.co.uk

Relevant StackOverflow question: https://stackoverflow.com/q/52471237/1593077

Consider the following program:

  #include <iostream>
  #include <thread> 

  using std::cout;
  using std::endl;

  template <typename T>
  class Something {
      struct TLBookkeeping {
          TLBookkeeping() {
              std::cout << "TLBookkeeping() " << std::this_thread::get_id() <<

      static void foo();
      static thread_local TLBookkeeping bookkeeping_;

  template <typename T>
  thread_local typename Something<T>::TLBookkeeping Something<T>::bookkeeping_;

  template <typename T>
  void Something<T>::foo() {
      std::cout << &bookkeeping_ << std::endl;
      std::cout << &bookkeeping_ << std::endl;

  namespace {
  struct Struct {};

  int main() {    

If we compile and run it, we get:

  TLBookkeeping() 140713161373504
  TLBookkeeping() 140713161373504

i.e. the static TLBookeeping variable is constructed twice. IIANM, this
shouldn't happen; and compiling with GCC doesn't cause this behavior.

You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20190603/751d7333/attachment.html>

More information about the llvm-bugs mailing list