macOSX 线程明确标记为已删除



我正在构建一个具有 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 threadn";
    }
};
int main (int argc, char* argv[]) 
{
  Functor functor;
  std::thread thread (&Functor::execute, std::ref(functor));
  thread.join();
}

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

$ g++ -std=c++11 -Wall -pthread test_thread.cpp -o test_thread

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

$ clang++ -std=c++11 -Wall -pthread test_thread.cpp -o test_thread

但是在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.

我不知道如何解决这个问题,这对我来说似乎是一个编译器错误。作为参考,该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

知道我做错了什么吗?谢谢!唐纳德。

该标准不要求std::thread构造函数(或与此相关的类似std::async)在作为第一个参数传递时解开reference_wrapper的包装,并带有指向成员函数的指针std::bind。传递指向Functor的指针,而不是reference_wrapper。(请参阅库活动问题列表 DR2219。

最新更新