C ++ boost :: thread,如何在类中启动一个线程

时间:2012-02-26 23:09:36

标签: c++ linux multithreading boost

如何在对象中启动线程?例如,

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()); ...

以后我可以做点什么,

abc.thread.interrupt();
abc.thread.join();

感谢。

2 个答案:

答案 0 :(得分:19)

既不需要绑定也不需要指针。

boost::thread m_thread;
//...
m_thread = boost::thread(&ABC::Start, abc);

答案 1 :(得分:5)

使用boost.bind:

boost::thread(boost::bind(&ABC::Start, abc));

你可能想要一个指针(或shared_ptr):

boost::thread* m_thread;
m_thread = new boost::thread(boost::bind(&ABC::Start, abc));