2014-11-24 84 views
3

我正在用C++ 11线程构建应用程序,但我似乎无法让它在MacOSX 10.9上与clang ++一起工作。这是我能找到的是最简单的例子引起的问题:macosx线程明确标记为删除

#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 

它还会编译和使用铛++(版本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. 

我不知道如何解决这个问题,它似乎是一个编译器bug。作为参考,在Mac上的铛的版本是:

$ 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 

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

+0

我不确定是你的问题的原因,但标准不需要'std :: thread'构造函数 - 或类似的'std :: async'来解封在这种情况下,'reference_wrapper'是'std :: bind'的方式。尝试将一个指针传递给'Functor'而不是'reference_wrapper'。 (请参见[图书馆活动问题列表DR2219](http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-active.html#2219)。) – Casey 2014-11-24 16:42:36

+0

感谢凯西,似乎确实解决了它 - 至少这个例子编译和运行没有任何问题。我将修改其余的代码以匹配,看看它是否确实为整个应用程序解决了问题。 – jdtournier 2014-11-24 16:49:06

+0

@Casey任何机会,你可以张贴您的评论作为答案,所以我可以接受它...? – jdtournier 2014-11-24 17:18:14

回答

1

该标准不要求std::thread构造 - 或与此有关的类似std::async - 当以与指针到成员函数的方式std::bind做的第一个参数传递给解开一个reference_wrapper。将指针传递给Functor而不是reference_wrapper。 (请参阅Library Active Issues list DR2219。)