c – 如何使用boost将类成员函数作为线程函数

前端之家收集整理的这篇文章主要介绍了c – 如何使用boost将类成员函数作为线程函数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我打算写一个适配器类.在这个类中有一个xmlrpc-c服务器(深渊服务器).我想通过创建一个新线程来启动服务器,而线程的函数是成员函数 XMLThreadFun().

当我尝试使用下面的代码时,在适配器的构造函数实现的行上有一个错误

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

谁能告诉我如何解决这个错误,或者如何实现我的目标?对此,我真的非常感激.

以下是我的代码片段:

  1. #ifdef _MSC_VER
  2. #pragma warning( disable : 4503 4355 4786 )
  3. #else
  4. #include "config.h"
  5. #endif
  6.  
  7. #include "quickfix/FileStore.h"
  8. #include "quickfix/SocketInitiator.h"
  9. #include "quickfix/SessionSettings.h"
  10. #include "Application.h"
  11. #include <string>
  12. #include <iostream>
  13. #include <fstream>
  14. #include "quickfix/SessionID.h"
  15. #include "quickfix/Session.h"
  16. #include "getopt-repl.h"
  17.  
  18.  
  19. #include <cassert>
  20. #include <xmlrpc-c/base.hpp>
  21. #include <xmlrpc-c/registry.hpp>
  22. #include <xmlrpc-c/server_abyss.hpp>
  23.  
  24.  
  25.  
  26. #include <boost/thread.hpp>
  27. #include <boost/date_time.hpp>
  28.  
  29. using namespace std;
  30. class theClient : public xmlrpc_c::method {
  31. public:
  32. theClient() {}
  33. theClient(FIX::SocketInitiator* initiator) {
  34. set<FIX::SessionID> s(initiator->getSessions());
  35. set<FIX::SessionID>::iterator myIterator;
  36. for (myIterator = s.begin(); myIterator != s.end(); myIterator++) {
  37. string str(myIterator->getSenderCompID());
  38. clientname = str;
  39. }
  40. }
  41.  
  42. void execute(xmlrpc_c::paramList const& paramList,xmlrpc_c::value * const retvalP) {
  43. *retvalP = xmlrpc_c::value_string(clientname);
  44. }
  45.  
  46. private:
  47. string clientname;
  48.  
  49. };
  50.  
  51. class Adapter {
  52. private:
  53. xmlrpc_c::registry myRegistry;
  54. xmlrpc_c::methodPtr XMLRPCMethodP;
  55. xmlrpc_c::serverAbyss webServer;
  56. boost::thread webServerThread;
  57. public:
  58. void initWebServer(string rpcHost,string rpcPort);
  59. void XMLThreadFun();
  60. Adapter(string rpcHost,string rpcPort);
  61. };
  62.  
  63. Adapter::Adapter(string rpcHost,string rpcPort) : myRegistry(),XMLRPCMethodP(new theClient),webServer(myRegistry,8181,"/tmp/xmlrpc_log"),webServerThread(boost::bind(&Adapter::XMLThreadFun,this,&webServer))
  64. {
  65. initWebServer(rpcHost,rpcPort);
  66. }
  67.  
  68. void Adapter::XMLThreadFun() {
  69. webServer->run();
  70. }
  71.  
  72. void Adapter::initWebServer(string rpcHost,string rpcPort) {
  73. webServerThread.join();
  74. }

解决方法

您将需要使用boost :: bind将成员函数作为线程调用.
就像是
  1. class MyClass {
  2. public:
  3. void Start();
  4. void DoStuff( int limit );
  5. };
  6.  
  7. MyClass foo;
  8. boost::thread thread1( boost::bind( &MyClass::Start,&foo ) );
  9. boost::thread thread2( boost::bind( &MyClass::DoStuff,&foo,30 ) );
  10. // threads do stuff here
  11. thread1.join();
  12. thread2.join();

具体来说,看起来你会改变

  1. webServerThread( boost::bind( &Adapter::XMLThreadFun,&webServer)

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

猜你在找的C&C++相关文章