Hi,

I am using Thrift 0.8.0 in C++ code, TNonblockingServer with ThreadManager, 
which processes MyService. But there're some other objects(like DB connectors) 
will be used in MyServiceHanler, so one object(like DB connector) for each 
thread is best of all, while there's only one ServiceHandler.

My sample code is listed below, testHandler::sleep want an DB connector owned 
by the thread from which is processed, so How can i got it? 
Is there any way to pass args to the threads, or do you have any other 
solutions?

Thank you very much!

File test.thrift:
service test {
    bool sleep(1:i32 time);
}


File TestCppServer.cpp:

#include "test.h"
#include <arpa/inet.h>
#include <protocol/TBinaryProtocol.h>
#include <server/TNonblockingServer.h>
#include <transport/TServerSocket.h>
#include <transport/TBufferTransports.h>

#include <concurrency/ThreadManager.h>
#include <concurrency/PosixThreadFactory.h>

#include "unistd.h"
//#include <arpa/inet.h>

using namespace ::apache::thrift;
using namespace ::apache::thrift::protocol;
using namespace ::apache::thrift::transport;
using namespace ::apache::thrift::server;
using namespace ::apache::thrift::concurrency;

using boost::shared_ptr;

class testHandler : virtual public testIf {
 public:
  testHandler() {
    printf("Server is on!");
      // Your initialization goes here
  }

  bool sleep(const int32_t time) {
    //usleep(time);
    return true;
  }

};

int main(int argc, char **argv) {
    int port = 9090;
    shared_ptr<testHandler> handler(new testHandler());
    shared_ptr<TProcessor> processor(new testProcessor(handler));
    shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());

    shared_ptr<ThreadManager> threadManager = 
ThreadManager::newSimpleThreadManager(5);
    shared_ptr<PosixThreadFactory> threadFactory = 
shared_ptr<PosixThreadFactory>(new PosixThreadFactory());
    threadManager->threadFactory(threadFactory);
    threadManager->start();
    TNonblockingServer server(processor, protocolFactory, port, threadManager);
    server.serve();

    return 0;
}




Xie Ling
2012-02-20 

Reply via email to