macosx线程明确标记为已删除

时间:2014-11-24 16:31:17

标签: multithreading macos 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();
}

使用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

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

1 个答案:

答案 0 :(得分:1)

该标准不要求std::thread构造函数 - 或类似的std::async - 在作为第一个参数传递时使用指向成员的指针展开reference_wrapperstd::bind的方式运作。将指针传递给Functor而不是reference_wrapper。 (见Library Active Issues list DR2219。)