[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


https://bugs.llvm.org/show_bug.cgi?id=42111

            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 {
  public:
      struct TLBookkeeping {
          TLBookkeeping() {
              std::cout << "TLBookkeeping() " << std::this_thread::get_id() <<
std::endl;
         }
      }; 

      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() {    
      Something<Struct>::foo();
  }


If we compile and run it, we get:

  TLBookkeeping() 140713161373504
  0x7ffa55ffb73f
  TLBookkeeping() 140713161373504
  0x7ffa55ffb73f

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