2014-11-24 59 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。)