在带有clang的Mac OS X上创建std :: thread时出错:

编程入门 行业动态 更新时间:2024-10-28 18:32:31
本文介绍了在带有clang的Mac OS X上创建std :: thread时出错:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

考虑我的测试代码:

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

And if I create a thread like this: _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 ++.

What am I doing wrong? I don't have such problem on Windows with VS2012. I also didn't have this problem with default stdlib implementation on Mac, but now I have to use libc++.

我的编译器标志: -std=c++11 -mmacosx-version-min=10.7 -stdlib=libc++

推荐答案

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

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

This tries to make a copy of *this to store in the new thread object, but your type is not copyable because its member _th is not copyable.

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

You probably want to store a pointer to the object, not a copy of the object:

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

您的程序将终止,因为您没有加入线程.在类型的析构函数中,您应该执行以下操作:

N.B. your program will terminate because you do not join the thread. In your type's destructor you should do something like:

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

更多推荐

在带有clang的Mac OS X上创建std :: thread时出错:

本文发布于:2023-10-23 12:19:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1520798.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Mac   clang   OS   thread   std

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!