macosx 线程显式标记为已删除

jdt*_*ier 5 macos multithreading c++11 clang++

我正在使用 C++11 线程构建一个应用程序,但我似乎无法让它在 MacOSX 10.9 上与 clang++ 一起工作。这是我能找到的导致问题的最简单示例:

#include <thread>
#include <iostream>

class Functor {
  public:
    Functor() = default;
    Functor (const Functor& ) = delete;
    void execute () { 
      std::cerr << "running in thread\n";
    }
};

int main (int argc, char* argv[]) 
{
  Functor functor;
  std::thread thread (&Functor::execute, std::ref(functor));
  thread.join();
}
Run Code Online (Sandbox Code Playgroud)

这在 Arch Linux 上使用 g++(版本 4.9.2)和以下命令行编译并运行良好:

$ g++ -std=c++11 -Wall -pthread test_thread.cpp -o test_thread
Run Code Online (Sandbox Code Playgroud)

它还可以使用 clang++(版本 3.5.0,也在 Arch Linux 上)编译并运行良好:

$ clang++ -std=c++11 -Wall -pthread test_thread.cpp -o test_thread
Run Code Online (Sandbox Code Playgroud)

但在 MacOSX 10.9.5 上失败,使用 XCode 6.1(无论我是否包含 -stdlib=libc++ 选项):

$ clang++ -std=c++11 -Wall -pthread test_thread.cpp -o test_thread
In file included from test_thread.cpp:1:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/thread:332:5: error: attempt to use a deleted function
    __invoke(_VSTD::move(_VSTD::get<0>(__t)), _VSTD::move(_VSTD::get<_Indices>(__t))...);
    ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/thread:342:5: note: in instantiation of function template specialization
      'std::__1::__thread_execute<void (Functor::*)(), std::__1::reference_wrapper<Functor> , 1>' requested here
    __thread_execute(*__p, _Index());
    ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/thread:354:42: note: in instantiation of function template specialization
      'std::__1::__thread_proxy<std::__1::tuple<void (Functor::*)(), std::__1::reference_wrapper<Functor> > >' requested here
    int __ec = pthread_create(&__t_, 0, &__thread_proxy<_Gp>, __p.get());
                                         ^
test_thread.cpp:19:15: note: in instantiation of function template specialization 'std::__1::thread::thread<void (Functor::*)(), std::__1::reference_wrapper<Functor> , void>'
      requested here
  std::thread thread (&Functor::execute, std::ref(functor));
              ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/type_traits:1001:5: note: '~__nat' has been explicitly marked deleted
      here
    ~__nat() = delete;
    ^
1 error generated.
Run Code Online (Sandbox Code Playgroud)

我不知道如何解决这个问题,对我来说这似乎是一个编译器错误。作为参考,该 Mac 上的 clang 版本是:

$ clang++ --version
Apple LLVM version 6.0 (clang-600.0.54) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin13.4.0
Thread model: posix
Run Code Online (Sandbox Code Playgroud)

任何想法我做错了什么?谢谢!唐纳德。

Cas*_*sey 2

该标准不要求std::thread构造函数(或类似的构造函数)在使用指向成员函数的指针作为第一个参数传递时std::async解开 a 。传递一个指针 to而不是. (请参阅库活跃问题列表 DR2219。)reference_wrapperstd::bindFunctorreference_wrapper