Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ boost::thread, how to start a thread inside a class

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.

like image 691
2607 Avatar asked Feb 26 '12 23:02

2607


2 Answers

You need neither bind, nor pointer.

boost::thread m_thread;
//...
m_thread = boost::thread(&ABC::Start, abc);
like image 134
Igor R. Avatar answered Sep 28 '22 00:09

Igor R.


Use boost.bind:

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

You probably want a pointer (or a shared_ptr):

boost::thread* m_thread;
m_thread = new boost::thread(boost::bind(&ABC::Start, abc));
like image 41
Guy Sirton Avatar answered Sep 28 '22 01:09

Guy Sirton