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

How can I start a stream inside an object? For instance,

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?, ... ... eg, abc.m_thread = boost::thread(&abc.Start()); ... 

So later I can do something like

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

Thanks.

+6
source share
2 answers

Use boost.bind:

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

You probably need a pointer (or shared_ptr):

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

You need neither bind nor a pointer.

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

Source: https://habr.com/ru/post/909416/


All Articles