2013-08-19 33 views
10

考慮我的測試代碼:錯誤在Mac OS X創建的std ::線程鏗鏘: 「嘗試使用刪除功能」

#include <thread> 

class Foo { 
public: 
    void threadFunc() {} 
    void startThread() { 
     _th = std::thread(&Foo::threadFunc, *this); 
    } 
private: 
    std::thread _th; 
}; 

int main(int argc, char *argv[]) 
{  
    Foo f; 
    f.startThread(); 
    return 0; 
} 

這是它會產生一個錯誤:

../untitled/main.cpp:13:14: warning: unused parameter 'argc' [-Wunused-parameter] 
int main(int argc, char *argv[]) 
      ^
../untitled/main.cpp:13:26: warning: unused parameter 'argv' [-Wunused-parameter] 
int main(int argc, char *argv[]) 
         ^
In file included from ../untitled/main.cpp:1: 
In file included from /usr/bin/../lib/c++/v1/thread:90: 
In file included from /usr/bin/../lib/c++/v1/__functional_base:15: 
/usr/bin/../lib/c++/v1/type_traits:1372:12: error: call to implicitly-deleted copy constructor of 'typename decay<Foo &>::type' (aka 'Foo') 
    return _VSTD::forward<_Tp>(__t); 
      ^~~~~~~~~~~~~~~~~~~~~~~~ 
/usr/bin/../lib/c++/v1/__config:273:15: note: expanded from macro '_VSTD' 
#define _VSTD std::_LIBCPP_NAMESPACE 
      ^
/usr/bin/../lib/c++/v1/thread:351:33: note: in instantiation of function template specialization 'std::__1::__decay_copy<Foo &>' requested here 
           __decay_copy(_VSTD::forward<_Args>(__args))...)); 
           ^
../untitled/main.cpp:7:15: note: in instantiation of function template specialization 'std::__1::thread::thread<void (Foo::*)(), Foo &, void>' requested here 
     _th = std::thread(&Foo::threadFunc, *this); 
      ^
../untitled/main.cpp:10:17: note: copy constructor of 'Foo' is implicitly deleted because field '_th' has an inaccessible copy constructor 
    std::thread _th; 
       ^

如果我創建這樣一個主題:_th = std::thread(&Foo::threadFunc, std::ref(*this));

我得到:

../untitled/main.cpp:13:14: warning: unused parameter 'argc' [-Wunused-parameter] 
int main(int argc, char *argv[]) 
      ^
../untitled/main.cpp:13:26: warning: unused parameter 'argv' [-Wunused-parameter] 
int main(int argc, char *argv[]) 
         ^
In file included from ../untitled/main.cpp:1: 
/usr/bin/../lib/c++/v1/thread:330:5: error: attempt to use a deleted function 
    __invoke(_VSTD::move(_VSTD::get<0>(__t)), _VSTD::move(_VSTD::get<_Indices>(__t))...); 
    ^
/usr/bin/../lib/c++/v1/thread:340:5: note: in instantiation of function template specialization 'std::__1::__threaad_execute<void (Foo::*)(), std::__1::reference_wrapper<Foo> , 1>' requested here 
    __threaad_execute(*__p, _Index()); 
    ^
/usr/bin/../lib/c++/v1/thread:352:41: note: in instantiation of function template specialization 'std::__1::__thread_proxy<std::__1::tuple<void (Foo::*)(), std::__1::reference_wrapper<Foo> > >' requested here 
    int __ec = pthread_create(&__t_, 0, &__thread_proxy<_Gp>, __p.get()); 
             ^
../untitled/main.cpp:7:15: note: in instantiation of function template specialization 'std::__1::thread::thread<void (Foo::*)(), std::__1::reference_wrapper<Foo> , void>' requested here 
     _th = std::thread(&Foo::threadFunc, std::ref(*this)); 
      ^
/usr/bin/../lib/c++/v1/type_traits:833:5: note: function has been explicitly marked deleted here 
    ~__nat() = delete; 
    ^

我在做什麼錯?我在VS2012的Windows上沒有這樣的問題。我也沒有在Mac上使用默認的stdlib實現這個問題,但現在我必須使用libC++。

我的編譯器標誌: -std=c++11 -mmacosx-version-min=10.7 -stdlib=libc++

+1

的(符合標準的)版本'的std :: ref' [上Coliru正常工作(http://coliru.stacked-crooked.com/view ?id = 61d9f8137948ff6540e8c2235ff01bdd-e1204655eaff68246b392dc70c5a32c9),您可能偶然發現了編譯器/ stdlib中的錯誤。 – Casey

回答

11
_th = std::thread(&Foo::threadFunc, *this); 

這試圖使*this副本在新線程對象來存儲,但由於其成員_th是不可拷貝你的類型是不可拷貝。

你可能想一個存儲指向對象,而不是對象的副本:

_th = std::thread(&Foo::threadFunc, this); 

注:你的程序將因爲你不加入線程而終止。在你的類型的析構函數,你應該這樣做:

~Foo() { if (_th.joinable()) _th.join(); } 
+0

我以爲'線程'期望對類的引用,而不是指針。謝謝。 –

+0

不,它期望任何可調用類型,並且可以使用'Foo'或'Foo&'或'Foo *'類型的參數調用'&Foo :: threadFunc'等成員函數指針。如果你願意,你可以傳遞一個引用,但這不是你的代碼所做的。如果你想通過一個引用使用'std :: thread(&Foo :: threadFunc,std :: ref(* this));'所以它不會嘗試複製。 –

+1

正如你在問題中所看到的,我也嘗試過'std :: ref',它也沒有工作。 –

相關問題