1:头文件
#include <thrift/concurrency/ThreadManager.h>
#include <thrift/concurrency/PosixThreadFactory.h>
#include <thrift/server/TThreadPoolServer.h>
#include <thrift/server/TThreadedServer.h>
2:namespace
using namespace ::apache::thrift::concurrency;
3:源代码
int port = 9090;
shared_ptr<serDemoHandler> handler(new serDemoHandler());
shared_ptr<TProcessor> processor(new serDemoProcessor(handler));
shared_ptr<TServerTransport> serverTransport(new TServerSocket(port));
shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
shared_ptr<ThreadManager> threadManager = ThreadManager::newSimpleThreadManager(CMyIni::instance()->m_maxPool);//指定10个线程数
shared_ptr<PosixThreadFactory> threadFactory = shared_ptr<PosixThreadFactory>(new PosixThreadFactory());
threadManager->threadFactory(threadFactory);
threadManager->start();
TThreadPoolServer server(processor,serverTransport,transportFactory,protocolFactory,threadManager);//使用线程池
//TNonblockingServer server(processor,protocolFactory,port,threadManager);//使用线程池
//TNonblockingServer server(processor,protocolFactory,port);//不使用线程池
server.serve();
4:over
获取更多帮主请关注小程序