Je vais écrire une classe d’adaptateur. Dans cette classe, il y a un serveur xmlrpc-c (serveur abyss). Je veux démarrer le serveur en créant un nouveau thread, et la fonction du thread est la fonction membre XMLThreadFun()
.
Lorsque j’essaie d’utiliser le code ci-dessous, il y a une erreur à la ligne de l’implémentation du constructeur de l’adaptateur:
/usr/include/boost/bind/bind.hpp:69:37: error: 'void (Adapter::*)()' is not a class, struct, or union type
Quelqu’un peut-il me dire comment résoudre cette erreur, ou comment atteindre mon objective? J’apprécie vraiment cela.
Voici mon extrait de code:
#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 #include #include #include "quickfix/SessionID.h" #include "quickfix/Session.h" #include "getopt-repl.h" #include #include #include #include #include #include using namespace std; class theClient : public xmlrpc_c::method { public: theClient() {} theClient(FIX::SocketInitiator* initiator) { set s(initiator->getSessions()); set::iterator myIterator; for (myIterator = s.begin(); myIterator != s.end(); myIterator++) { ssortingng str(myIterator->getSenderCompID()); clientname = str; } } void execute(xmlrpc_c::paramList const& paramList, xmlrpc_c::value * const retvalP) { *retvalP = xmlrpc_c::value_ssortingng(clientname); } private: ssortingng clientname; }; class Adapter { private: xmlrpc_c::registry myRegistry; xmlrpc_c::methodPtr XMLRPCMethodP; xmlrpc_c::serverAbyss webServer; boost::thread webServerThread; public: void initWebServer(ssortingng rpcHost, ssortingng rpcPort); void XMLThreadFun(); Adapter(ssortingng rpcHost, ssortingng rpcPort); }; Adapter::Adapter(ssortingng rpcHost, ssortingng 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(ssortingng rpcHost, ssortingng rpcPort) { webServerThread.join(); }
Vous devrez utiliser boost :: bind pour appeler une fonction membre en tant que thread. Quelque chose comme
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();
Plus précisément, il semble que vous changiez
webServerThread( boost::bind( &Adapter::XMLThreadFun, this, &webServer)
à
webServerThread( boost::bind( &Adapter::XMLThreadFun, this )
Pas besoin d’utiliser boost :: bind
boost::thread thread2( boost::bind( &MyClass::DoStuff, &foo, 30 ) );
est équivalent à:
boost::thread thread2( &MyClass::DoStuff, &foo, 30 );