为什么Apple clang禁止C ++ 11 thread_local官方' clang支持它

时间:2015-01-22 17:16:21

标签: xcode c++11 clang

下面是一个简单的程序,它在共享库中使用非POD类型的C ++ 11 thread_local变量进行测试。

如果我使用自制软件,这样可以正常工作:

> /usr/local/Cellar/llvm/3.5.0_2/bin/clang --version                                          
clang version 3.5.0 (tags/RELEASE_350/final)
Target: x86_64-apple-darwin14.0.0
Thread model: posix

> cmake .. -G Ninja -DCMAKE_C_COMPILER=/usr/local/Cellar/llvm/3.5.0_2/bin/clang -DCMAKE_CXX_COMPILER=/usr/local/Cellar/llvm/3.5.0_2/bin/clang++
-- The C compiler identification is Clang 3.5.0
-- The CXX compiler identification is Clang 3.5.0
-- Check for working C compiler using: Ninja
-- Check for working C compiler using: Ninja -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler using: Ninja
-- Check for working CXX compiler using: Ninja -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Configuring done
-- Generating done
> ninja all
...                                                                                      

>  ./main                                                                                       
XXX LifeCycle::LifeCycle 0x7fedc0c04b90
X before: -17
XXX LifeCycle::LifeCycle 0x7fedc0c04c10
X before in thread: -17
X after in thread: 2
XXX LifeCycle::~LifeCycle 0x7fedc0c04c10
X after: 1
XXX LifeCycle::~LifeCycle 0x7fedc0c04b90

但是,如果我尝试使用Apple Clang,我会收到一条错误消息,说明它不受支持:

> /usr/bin/clang --version
Apple LLVM version 6.0 (clang-600.0.56) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin14.0.0
Thread model: posix
> cmake .. -G Ninja -DCMAKE_C_COMPILER=/usr/bin/clang -DCMAKE_CXX_COMPILER=/usr/bin/clang++
-- The C compiler identification is AppleClang 6.0.0.6000056
-- The CXX compiler identification is AppleClang 6.0.0.6000056
-- Check for working C compiler using: Ninja
-- Check for working C compiler using: Ninja -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler using: Ninja
-- Check for working CXX compiler using: Ninja -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Configuring done
-- Generating done
-- Build files have been written to:

> ninja all
[1/4] Building CXX object CMakeFiles/lib.dir/lib.cpp.o
FAILED: /usr/bin/clang++   -Dlib_EXPORTS -Wall -std=c++11 -mmacosx-version-min=10.7 -stdlib=libc++ -fPIC -MMD -MT CMakeFiles/lib.dir/lib.cpp.o -MF CMakeFiles/lib.dir/lib.cpp.o.d -o CMakeFiles/lib.dir/lib.cpp.o -c ../lib.cpp
../lib.cpp:23:5: error: thread-local storage is unsupported for the current target
    thread_local LifeCycle lc;
    ^
1 error generated.
ninja: build stopped: subcommand failed.

除非底层编译器支持,并且生成的代码似乎有效,否则任何人都可以提供任何有关Apple's clang变种懦弱拒绝遵守thread_local的原因的见解吗?

lib.h:

#pragma once

int doit(int) __attribute__((__visibility__("default")));

lib.cpp:

#include "lib.h"

#include <thread>
#include <cstdlib>
#include <cstdio>

namespace {

    class LifeCycle {
    public:
        LifeCycle()
            : x(-17) {
            printf("XXX LifeCycle::LifeCycle %p\n", this);
        }

        ~LifeCycle() {
            printf("XXX LifeCycle::~LifeCycle %p\n", this);
        }

        int x;
    };

    thread_local LifeCycle lc;
} // namespace

int doit(int arg) {
    printf("X before: %d\n", lc.x);
    lc.x = arg;
    std::thread xwriter([arg]() {
            if (lc.x == arg)
                abort();
            printf("X before in thread: %d\n", lc.x);
            lc.x = arg + 1;
            printf("X after in thread: %d\n", lc.x);
        });
    xwriter.join();
    printf("X after: %d\n", lc.x);
    return (lc.x == arg ? EXIT_SUCCESS : EXIT_FAILURE);
}

main.cpp中:

#include "lib.h"

int main(int argc, char* argv[]) {
    return doit(argc);
}

的CMakeLists.txt:

cmake_minimum_required(VERSION 3.1)

set(CMAKE_CXX_FLAGS "-Wall -std=c++11 -mmacosx-version-min=10.7 -stdlib=libc++")

add_library(lib SHARED lib.cpp)
add_executable(main main.cpp)
target_link_libraries(main lib)

2 个答案:

答案 0 :(得分:42)

Xcode 8及更高版本中包含的clang编译器支持C ++ 11 thread_local关键字。从the WWDC 2016 video "What's New in LLVM"开始,the 5:50 mark中讨论了此功能已添加到Xcode 8测试版中。 (external transcript

问题中列出的示例程序在OS X 10.11.6下编译并运行Xcode 8 GM并生成预期输出。随后在macOS 10.13.4下使用Xcode 9.3,在macOS 10.14.4下使用Xcode 10.2.1进行了重新测试,并继续按预期运行。

关于iOS,我通过实验发现iOS 9及更高版本支持thread_local,但iOS 8.4或更早版本不支持。

对于Xcode 7.x及更早版本,以下是2014年苹果工程师在旧Apple开发者论坛上的答案(不再可访问):

  

我们不支持开源的thread_local实现   Clang因为我们相信我们可以提供更高的性能   使用动态中的各种功能实现我们的平台   连接。这样的实现将与ABI不兼容   在开源Clang中实现,所以我们不会得到支持   thread_local直到我们得到一个我们可以忍受的实现   可预见的未来。

后续帖子确认Xcode 6.3中仍然不支持thread_local

答案 1 :(得分:1)

根据http://clang.llvm.org/cxx_status.html

  

thread_local支持当前需要C ++运行时库   g ++ - 4.8或更高版本

我相信自制的clang版本使用不同的C ++运行时。