Linux C++ boost::thread,如何在类中启动线程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9458186/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:50:27 来源:igfitidea点击:
C++ boost::thread, how to start a thread inside a class
提问by 2607
How can I start a thread inside an object? For example,
如何在对象内启动线程?例如,
class ABC
{
public:
void Start();
double x;
boost::thread m_thread;
};
ABC abc;
... do something here ...
... how can I start the thread with Start() function?, ...
... e.g., abc.m_thread = boost::thread(&abc.Start()); ...
So that later I can do something like,
以便以后我可以做类似的事情,
abc.thread.interrupt();
abc.thread.join();
Thanks.
谢谢。
采纳答案by Guy Sirton
Use boost.bind:
使用 boost.bind:
boost::thread(boost::bind(&ABC::Start, abc));
You probably want a pointer (or a shared_ptr):
您可能想要一个指针(或 shared_ptr):
boost::thread* m_thread;
m_thread = new boost::thread(boost::bind(&ABC::Start, abc));
回答by Igor R.
You need neither bind, nor pointer.
您既不需要绑定,也不需要指针。
boost::thread m_thread;
//...
m_thread = boost::thread(&ABC::Start, abc);