Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make class member function as thread function using boost

I am going to write an adapter class. In this class there is an xmlrpc-c server (abyss server). I want to start the server by creating a new thread, and the thread's function is the member function XMLThreadFun().

When I try to use the code below there is an error at the line of the adapter's constructor implementation:

/usr/include/boost/bind/bind.hpp:69:37: error: ‘void (Adapter::*)()’ is not a class, struct, or union type

Can anyone tell me how to solve this error, or how to achieve my goal? I really appreciate it.

Below is my code snippet:

#ifdef _MSC_VER
#pragma warning( disable : 4503 4355 4786 )
#else
#include "config.h"
#endif

#include "quickfix/FileStore.h"
#include "quickfix/SocketInitiator.h"
#include "quickfix/SessionSettings.h"
#include "Application.h"
#include <string>
#include <iostream>
#include <fstream>
#include "quickfix/SessionID.h"
#include "quickfix/Session.h"
#include "getopt-repl.h"


#include <cassert>
#include <xmlrpc-c/base.hpp>
#include <xmlrpc-c/registry.hpp>
#include <xmlrpc-c/server_abyss.hpp>



#include <boost/thread.hpp>
#include <boost/date_time.hpp>

using namespace std;
class theClient : public xmlrpc_c::method {
public:
    theClient() {}
    theClient(FIX::SocketInitiator* initiator) {
        set<FIX::SessionID> s(initiator->getSessions());
        set<FIX::SessionID>::iterator myIterator;
        for (myIterator = s.begin(); myIterator != s.end(); myIterator++) {
            string str(myIterator->getSenderCompID());
            clientname = str;
        }
    }

    void execute(xmlrpc_c::paramList const& paramList,
        xmlrpc_c::value *   const  retvalP) {
        *retvalP = xmlrpc_c::value_string(clientname);
    }

private:
    string clientname;

};

class Adapter {
private:
    xmlrpc_c::registry myRegistry;
    xmlrpc_c::methodPtr XMLRPCMethodP;
    xmlrpc_c::serverAbyss webServer;
    boost::thread webServerThread;
public:
    void initWebServer(string rpcHost, string rpcPort);
    void XMLThreadFun();
    Adapter(string rpcHost, string rpcPort);
};

Adapter::Adapter(string rpcHost, string rpcPort) : myRegistry(), XMLRPCMethodP(new theClient), webServer(myRegistry, 8181, "/tmp/xmlrpc_log"), webServerThread(boost::bind(&Adapter::XMLThreadFun, this, &webServer))
{
    initWebServer(rpcHost, rpcPort);
}

void Adapter::XMLThreadFun() {
    webServer->run();
}

void Adapter::initWebServer(string rpcHost, string rpcPort) {
    webServerThread.join();
}
like image 444
Peiti Li Avatar asked Jul 15 '11 15:07

Peiti Li


2 Answers

You will need to use boost::bind to call a member function as a thread. Something like

class MyClass {
public: 
   void Start();
   void DoStuff( int limit );
};

MyClass foo;
boost::thread thread1( boost::bind( &MyClass::Start, &foo ) );
boost::thread thread2( boost::bind( &MyClass::DoStuff, &foo, 30 ) );
// threads do stuff here
thread1.join();
thread2.join();

Specifically here, it looks like you would change

webServerThread( boost::bind( &Adapter::XMLThreadFun, this, &webServer)

to

webServerThread( boost::bind( &Adapter::XMLThreadFun, this )
like image 173
Josh Avatar answered Sep 18 '22 02:09

Josh


No need to use boost::bind

boost::thread thread2( boost::bind( &MyClass::DoStuff, &foo, 30 ) );

is equivalent to:

boost::thread thread2( &MyClass::DoStuff, &foo, 30  );
like image 44
dielor Avatar answered Sep 20 '22 02:09

dielor