2011-07-15 16 views
6

アダプタクラスを記述します。このクラスには、xmlrpc-cサーバー(abyssサーバー)があります。新しいスレッドを作成してサーバーを起動したいのですが、そのスレッドの関数はメンバ関数XMLThreadFun()です。ブーストを使用してクラスメンバ関数をスレッド関数として作成する方法

私は以下のコードを使用しようとすると、アダプタのコンストラクタの実装の行でエラーがある:

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

誰がどのようにこのエラーを解決するために、または私の目標を達成する方法を教えてもらえますか?ほんとうにありがとう。あなたがスレッドとしてメンバ関数を呼び出すために、ブースト::バインドを使用する必要があります

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

答えて

8

は、以下の私のコードスニペットです。あなたは

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

があるバインド::

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

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

実際には、boost :: bindを使用しました。コンストラクタの行の最後を参照してください:アダプタ::アダプタ(文字列rpcHost、文字列rpcPort):myRegistry()、XMLRPCMethodP(new theClient)、webServer(myRegistry、8181、 "/ tmp/xmlrpc_log")、webServerThread(boost :: bind &Adapter :: XMLThreadFun、this、&webServer)) –

+0

Ah - 引数が一致しません。 XMLThreadFunは引数をとりませんが、あなたの呼び出しでwebServerへのポインタを指定しています。 – Josh

5

ブーストを使用する必要はありませんが変わってしまうように具体的にここに

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

よう ものは、それが見えますに相当:

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

これは、ジョシュの答えのコメント(または補足)ですか? – jogojapan

関連する問題