1

Given object x, I'd like to launch a new thread to invoke x.a() or even x.a(1,2,3). I can use boost::thread to do this for a non-member function; but how do I do this for a member function? How do I pass the this pointer?

In general, there's lots of STL and Boost code and templates which take code as a parameter, but, as this is not really defined in C++ (functions aren't first class vals, no native lambda support), I'm very confused how they're defined. I can do trial and error but I'd like something cleaerer and more reliable.

UPDATE: My question concerns primarily passing a method; when I try to do what would be the obvious syntax (ClassName::method_name, instance) I get invalid use of non-static member function error: no matching function to call. It would help if you could show the proper syntax to use Boost.thread on a non-static method.

UPDATE 2: I found why I'm struggling with this. The answers given, such as by @OrDinari, work great for boost::thread. But, when I try using a thread_group, that is, thread_group::create_thread, I get this error:

 error: no matching function for call to ‘boost::thread_group::create_thread(void (C::*)(), C*)’

So:

  1. Why does it work for single threads but not thread groups?
  2. How do I use create a thread in a group to run a member function?
SRobertJames
  • 8,210
  • 14
  • 60
  • 107

2 Answers2

2

Here's an example:

        boost::thread _commandControl(&Client::commandControl, &client);

This create a new thread, using the client object method commandControl, from the Client instance named client.

and here a bit larger piece of the code, might be easier to understand:

Client client(host, port);
std::cout << "Pick interface (1) Terminal  (2) Menu:\n";
char interfaceMode;
std::cin >> interfaceMode;
while (!client.setMode(interfaceMode)) {
    std::cout << "\nInvalid choice! try again (1) For Terminal  (2)For Menu:";
    std::cin >> interfaceMode;
}
try {
    std::cin.ignore();
    if (client.connect()) {
        try {

            boost::thread _commandControl(&Client::commandControl, &client);
            boost::thread _checkQueue(&Client::checkQueue, &client);
            _checkQueue.join();
            if (client.checkAlive() == false)throw ConnectionDCexception();
            _commandControl.join();
            client.close();
        }
        catch (std::exception &e) {
            std::cout << "\n" << e.what();
        }
    }
    else {
        std::cout << "\nFailed to initiate connecting, exiting...";
    }
}

The main thing you are looking for is:

Client client(host, port);

boost::thread _commandControl(&Client::commandControl, &client);
boost::thread _checkQueue(&Client::checkQueue, &client);

each is a different thread, from the same instance (client) of the class Client.

Dinari
  • 2,487
  • 13
  • 28
0

If you use C++11, you can use lambdas:

#include <iostream>
#include <thread>

class Foo
{
public:
    void foo()
    {
        std::cout << "Foo thread: " << std::this_thread::get_id() << std::endl;
    }
};

int main() {
    std::cout << "Main thred: " << std::this_thread::get_id() << std::endl;

    Foo foo;
    std::thread t1( [&foo] () {foo.foo();} );
    t1.join();

    return 0;
}

You can replace std::thread to boost::thread.

gomons
  • 1,946
  • 14
  • 25