C++ std::线程子体的std::vector
我试图创建一个线程向量,其属性是销毁时自动连接。Stroustrup建议使用防护螺纹:C++ std::线程子体的std::vector,c++,multithreading,c++11,vector,stl,C++,Multithreading,C++11,Vector,Stl,我试图创建一个线程向量,其属性是销毁时自动连接。Stroustrup建议使用防护螺纹: struct guarded_thread : std::thread { using std::thread::thread; ~guarded_thread() { if (joinable()) join(); } }; 此保护线的工作原理类似于魅力: void f() { std::cerr << "f();" << std::endl; } int main
struct guarded_thread : std::thread {
using std::thread::thread;
~guarded_thread() { if (joinable()) join(); }
};
此保护线的工作原理类似于魅力:
void f() { std::cerr << "f();" << std::endl; }
int main() { guarded_thread gt(f); }
void f(){std::cerr似乎guarded_thread
缺少默认的移动构造函数,应该删除复制构造函数
这是一个有效的例子
guarded_thread(const guarded_thread&) = delete ;
guarded_thread(guarded_thread&&) = default ;
注意:您的代码正在调用一个已删除的函数,即std::thread
copy构造函数,并且也没有提供移动构造函数。copy构造函数已在基(线程)中删除。请求默认移动构造函数解决了问题,谢谢!
template<typename _Callable, typename... _Args>
explicit
thread(_Callable&& __f, _Args&&... __args)
{
#ifdef GTHR_ACTIVE_PROXY
__asm ("" : : "r" (&pthread_create));
#endif
_M_start_thread(_M_make_routine(std::__bind_simple(
std::forward<_Callable>(__f),
std::forward<_Args>(__args)...)));
}
guarded_thread(const guarded_thread&) = delete ;
guarded_thread(guarded_thread&&) = default ;