【问题标题】:How to make class member function as thread function using boost如何使用boost将类成员函数作为线程函数
【发布时间】:2011-10-06 06:59:12
【问题描述】:

我要写一个适配器类。在这个类中有一个 xmlrpc-c 服务器(abyss 服务器)。我想通过创建一个新线程来启动服务器,线程的函数是成员函数XMLThreadFun()

当我尝试使用下面的代码时,适配器的构造函数实现行出现错误:

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

谁能告诉我如何解决这个错误,或者如何实现我的目标?我真的很感激。

下面是我的代码sn-p:

#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();
}

【问题讨论】:

    标签: c++ linux boost xml-rpc boost-thread


    【解决方案1】:

    您将需要使用 boost::bind 将成员函数作为线程调用。 像

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

    特别是这里,看起来你会改变

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

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

    【讨论】:

    • 其实我用的是 boost::bind。查看构造函数行的末尾: Adapter::Adapter(string rpcHost, string rpcPort) : myRegistry(), XMLRPCMethodP(new theClient), webServer(myRegistry, 8181, "/tmp/xmlrpc_log"), webServerThread(boost::bind( &Adapter::XMLThreadFun, this, &webServer))
    • 啊 - 你的论点不匹配。 XMLThreadFun 不接受任何参数,但您在调用中指定了指向 webServer 的指针。
    【解决方案2】:

    不需要使用 boost::bind

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

    相当于:

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

    【讨论】:

    • 我想这是对 Josh 答案的评论(或补充)?
    猜你喜欢
    • 1970-01-01
    • 2011-01-01
    • 1970-01-01
    • 2013-06-13
    • 2020-11-14
    • 2011-06-23
    • 2023-01-26
    • 1970-01-01
    • 2021-07-10
    相关资源
    最近更新 更多