简体   繁体   中英

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.

You need neither bind, nor pointer.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM