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

11,208

Solution 1

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

Solution 2

You need neither bind, nor pointer.

boost::thread m_thread;
//...
m_thread = boost::thread(&ABC::Start, abc);
Share:
11,208
2607
Author by

2607

Updated on June 24, 2022

Comments

  • 2607
    2607 almost 2 years

    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.