2013-08-19 4 views
10

Rozważmy mój kod testowy:Błąd podczas tworzenia std :: wątek na Mac OS X z brzękiem: "próba wykorzystania usuniętą funkcję"

#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; 
} 

Jest to błąd produkuje:

../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; 
       ^

a jeśli tworzę wątek takiego: _th = std::thread(&Foo::threadFunc, std::ref(*this));

uzyskać:

../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; 
    ^

Co robię źle? Nie mam takiego problemu w systemie Windows z VS2012. Również nie miałem tego problemu z domyślną implementacją stdlib na Macu, ale teraz muszę użyć biblioteki libC++.

Moi flagi kompilatora: -std=c++11 -mmacosx-version-min=10.7 -stdlib=libc++

+1

The (zgodny ze standardami) wersja z 'std :: ref' [działa poprawnie na Coliru] (http://coliru.stacked-crooked.com/view ? id = 61d9f8137948ff6540e8c2235ff01bdd-e1204655eaff68246b392dc70c5a32c9), najprawdopodobniej natknąłeś się na błąd w kompilatorze/stdlib. – Casey

Odpowiedz

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

to próbuje zrobić kopię *this zapisać w nowym obiekcie wątku, ale nie jest w twoim typie copyable bo jego członek _th nie jest copyable.

Prawdopodobnie chcesz przechowywać wskaźnik do obiektu, a nie kopii obiektu:

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

nb Twój program zostanie zakończony, ponieważ nie dołączysz do wątku. W destruktora Twojego typu powinien zrobić coś takiego:

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

Myślałem, że 'thread' oczekuje odniesienia do klasy, a nie wskaźnika. Dzięki. –

+0

Nie, oczekuje dowolnego typu wywoływanego, a wskaźnik funkcji elementu, taki jak '& Foo :: threadFunc' może być wywołany z argumentem typu' Foo' lub 'Foo &' lub 'Foo *'. Możesz przekazać referencję, jeśli chcesz, ale to nie jest to, co robi twój kod. Jeśli chcesz przekazać referencję, użyj 'std :: thread (& Foo :: threadFunc, std :: ref (* this));' to nie próbuje zrobić kopii. –

+1

Jak widać w pytaniu, próbowałem 'std :: ref' również i to nie działało. –