[llvm-bugs] [Bug 30347] New: thread_local variables not constructed/destructed on Windows

via llvm-bugs llvm-bugs at lists.llvm.org
Sun Sep 11 00:18:47 PDT 2016


            Bug ID: 30347
           Summary: thread_local variables not constructed/destructed on
           Product: clang
           Version: 3.9
          Hardware: PC
                OS: Windows NT
            Status: NEW
          Severity: normal
          Priority: P
         Component: C++11
          Assignee: unassignedclangbugs at nondot.org
          Reporter: ryan.prichard at gmail.com
                CC: dgregor at apple.com, llvm-bugs at lists.llvm.org
    Classification: Unclassified

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.


    #include <stdio.h>

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

    thread_local MyLocal mylocal;

    int main() {

The program should print:


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


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 at 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 at 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.

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/20160911/69750b0e/attachment-0001.html>

More information about the llvm-bugs mailing list