jonderry
jonderry

Reputation: 23633

How can I create a new thread from a c++ boost thread_group that begins execution in a member function of an object?

There are some member variables and mutexes associated with the object, so it would be easier to use a member function rather than a standalone function.

Upvotes: 0

Views: 198

Answers (1)

Cheers and hth. - Alf
Cheers and hth. - Alf

Reputation: 145194

Provide an operator() member func.

EDIT: like ...

#include <boost/thread.hpp>
#include <boost/bind.hpp>
#include <iostream>

struct MyThread
{
    int x;

    MyThread(): x( -1 ) {}
    void run()      // Name doesn't matter.
    {
        x = 42;
    }
};

int main()
{
    namespace b = boost;
    using namespace std;

    MyThread        t;
    b::thread_group g;

    g.create_thread( b::bind( &MyThread::run, &t ) ) ;
    // ... whatever
    g.join_all();
    cout << t.x << endl;    
}

Disclaimer: I'm unfamiliar with Boost threads. And this example added after answer was accepted.

Upvotes: 1

Related Questions