<html>
    <head>
      <base href="https://llvm.org/bugs/" />
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW --- - thread_local variables not constructed/destructed on Windows"
   href="https://llvm.org/bugs/show_bug.cgi?id=30347">30347</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>thread_local variables not constructed/destructed on Windows
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>3.9
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Windows NT
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>C++11
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>ryan.prichard@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>dgregor@apple.com, llvm-bugs@lists.llvm.org
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>On Windows, C++11 thread_local variables aren't constructed (or destructed)
unless the program links with some other MSVC object file that also uses
thread_local variables.

Example:

    #include <stdio.h>

    struct MyLocal {
        MyLocal() { printf("constructed\n"); }
        ~MyLocal() { printf("destructed\n"); }
        void method() { printf("invoked\n"); }
    };

    thread_local MyLocal mylocal;

    int main() {
        mylocal.method();
    }

The program should print:

    constructed
    invoked
    destructed

Instead, it only prints "invoked".  I'm using Clang 3.9 in a "VS2015 x64"
command window:

    >"c:\Program Files\LLVM\bin\clang.exe" test.cc -o test.exe

    >.\test
    invoked

AFAICT, MSVC 2013 does not support thread_local variables.  They were added in
MSVC 2015, which correctly calls the constructors.

I examined the object files produced by MSVC 2015.  I noticed that it includes
one of these arguments in the .drectve section:

    /include:___dyn_tls_init@12     [for 32-bit x86]
    /include:__dyn_tls_init         [for 64-bit x64]

My example code starts working if I add a pragma to the source:

    #pragma comment(linker, "/include:___dyn_tls_init@12")  [for 32-bit x86]
    #pragma comment(linker, "/include:__dyn_tls_init")      [for 64-bit x64]

Presumably Clang should also output this directive?

My specific toolchain versions:

    clang version 3.9.0 (branches/release_39)
    Target: x86_64-pc-windows-msvc
    Thread model: posix
    InstalledDir: c:\Program Files\LLVM\bin

    Visual Studio 2015 Update 3

    Microsoft (R) C/C++ Optimizing Compiler Version 19.00.24213.1 for x64
    Copyright (C) Microsoft Corporation.  All rights reserved.</pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>