C++方法线程
C++ Method thread
我想为类中的方法创建一个线程。我写过这个:
class Program
{
public:
Program();
void render();
};
Program::Program()
{
thread t(render);
}
void Program::render()
{
cout << "Hello" << endl;
}
这给了我这个错误消息:
"错误 C3867: '程序::渲染':函数调用缺少参数列表;使用"&Program::render"创建指向成员的指针"
当我写这个时:
thread t(&Program::render);
它说:
1>c:program files(x86)microsoft visual studio 12.0vcincludefunctional(1149) : error C2064 : term does not evaluate to a function taking 0 arguments
1> class does not define an 'operator()' or a user defined conversion operator to a pointer - to - function or reference - to - function that takes appropriate number of arguments
1> c:program files(x86)microsoft visual studio 12.0vcincludefunctional(1137) : see reference to function template instantiation 'void std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall Program::* )(void),void,Program,>,>::_Do_call<,>(std::tuple<>,std::_Arg_idx<>)' being compiled
1> c:program files(x86)microsoft visual studio 12.0vcincludefunctional(1137) : see reference to function template instantiation 'void std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall Program::* )(void),void,Program,>,>::_Do_call<,>(std::tuple<>,std::_Arg_idx<>)' being compiled
1> c:program files(x86)microsoft visual studio 12.0vcincludethrxthread(195) : see reference to function template instantiation 'void std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall Program::* )(void),void,Program,>,>::operator ()<>(void)' being compiled
1> c:program files(x86)microsoft visual studio 12.0vcincludethrxthread(195) : see reference to function template instantiation 'void std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall Program::* )(void),void,Program,>,>::operator ()<>(void)' being compiled
1> c:program files(x86)microsoft visual studio 12.0vcincludethrxthread(192) : while compiling class template member function 'unsigned int std::_LaunchPad<_Target>::_Run(std::_LaunchPad<_Target> *)'
1> with
1>[
1> _Target = std::_Bind<true, void, std::_Pmf_wrap<void(__thiscall Program::*)(void), void, Program, >, >
1>]
1> c:program files(x86)microsoft visual studio 12.0vcincludethrxthread(187) : see reference to function template instantiation 'unsigned int std::_LaunchPad<_Target>::_Run(std::_LaunchPad<_Target> *)' being compiled
1> with
1>[
1> _Target = std::_Bind<true, void, std::_Pmf_wrap<void(__thiscall Program::*)(void), void, Program, >, >
1>]
1> c:program files(x86)microsoft visual studio 12.0vcincludethrxthread(205) : see reference to class template instantiation 'std::_LaunchPad<_Target>' being compiled
1> with
1>[
1> _Target = std::_Bind<true, void, std::_Pmf_wrap<void(__thiscall Program::*)(void), void, Program, >, >
1>]
1> c:program files(x86)microsoft visual studio 12.0vcincludethread(49) : see reference to function template instantiation 'void std::_Launch<std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall Program::* )(void),void,Program,>,>>(_Thrd_t *,_Target &&)' being compiled
1> with
1>[
1> _Target = std::_Bind<true, void, std::_Pmf_wrap<void(__thiscall Program::*)(void), void, Program, >, >
1>]
1> c:userserikdocumentsvisual studio 2013projectsc++gameenginec++gameengineprogram.cpp(16) : see reference to function template instantiation 'std::thread::thread<void(__thiscall Program::* )(void),>(_Fn &&)' being compiled
1> with
1>[
1> _Fn = void(__thiscall Program::*)(void)
1>]
我不太明白出了什么问题。你能解释一下并给我一个解决方案吗?
render
是一个非静态成员函数,因此它需要一个对象来调用它。您需要将其包装在一个小函数对象中才能在Program
上调用它。根据口味,有两种方便的方法可以做到这一点:
thread t(&Program::render, this);
thread t([this]{render();});
下一个问题是线程是构造函数的本地线程,因此会立即销毁。在未加入线程对象(等待线程完成)或分离线程对象的情况下,不得销毁该对象。因此,要么使其成为会员,以便以后可以加入;或在返回之前将其拆下。
你可以这样做:
Program::Program()
{
std::thread t(&Program::render, this);
t.join();
}
但是,这没有任何效果,因为它只是阻塞,直到函数调用返回。也许你想分离线程?
std::thread(&Program::render, this).detach();
现在,您需要构建自己的逻辑来告诉线程何时停止。
或者,将线程设置为类成员:
struct Program
{
void render();
Program() : render_thread_(&Program::render, this) {}
~Program() { render_thread_.join(); }
std::thread render_thread_;
};
你应该使类成员函数static
,或者使用std::bind()
将函数传递给std::thread
实例。
另请注意,std::thread t;
变量是构造函数的局部变量。您将以这种方式失去与该线程的任何连接,并且在构造函数的作用域结束后不再控制它。
相关文章:
- 方法重写线程C++中的概念
- 使用基类中的派生方法运行线程,而无需使用模板
- 将线程中的数据存储到全局容器的最佳方法?
- 处理影响跨不同线程共享对象的定时回调的最佳方法是什么?
- 如何将带有参数的方法传递给线程以执行?
- C++ POCO - 如何在不使用 run() 方法的情况下启动线程池上的线程?
- 在销毁期间从另一个线程调用对象上调用方法是否未定义行为?
- C++中的多线程:连接线程的正确方法
- 使用包含互斥锁的类的方法实例化 cpp11 线程
- 如何在JNI中从线程内部调用JAVA方法
- 如何从另一个线程调用颤振引擎方法
- 在另一个线程上发出 QObject 信号的正确方法?
- 在C++线程内实现多个计时器的最安全方法
- C++ std::线程调用方法,从对象原因到调用此类的析构函数
- 将指针传递到另一个线程的正确方法
- 在另一个 QThread 上运行成员方法时,无法将事件发送到其他线程拥有的对象
- 支持多线程方法来构建数组中所有元素的集合吗
- 这两种线程方法之间有什么区别
- 什么是Qt中低延迟音频合成的最佳多线程方法
- 将不同的线程方法组合到Windows窗体应用程序中