使用clang在Mac OS X上创建std :: thread时出错:“尝试使用已删除的函数”

时间:2013-08-19 07:56:13

标签: multithreading macos c++11 clang stdthread

考虑我的测试代码:

#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 个答案:

答案 0 :(得分:11)

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

这会尝试制作*this的副本以存储在新的线程对象中,但您的类型不可复制,因为其成员_th不可复制。

您可能希望存储指向对象的指针,而不是对象的副本:

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

N.B。您的程序将终止,因为您没有加入该主题。在你的类型的析构函数中,你应该做类似的事情:

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