thrift多线程服务端
来源:互联网 发布:淘宝众筹怎么推广 编辑:程序博客网 时间:2024/05/16 19:37
接上一篇 thrift一个例子
作为服务端,应该能够同时接收多个客户端传来的数据,所以服务端应该实现多线程机制。
按以下3个步骤改写服务端(Serv_server.skeleton.cpp)即可实现多线程。
(1)采用线程池的main函数的代码如下:
int main(int argc, char **argv) {
// thread pool
shared_ptr<ServHandler> handler(new ServHandler());
shared_ptr<TProcessor> processor(new ServProcessor(handler));
shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
shared_ptr<TServerTransport> serverTransport(new TServerSocket(9090));
// 指定15个线程
shared_ptr<ThreadManager> threadManager = ThreadManager::newSimpleThreadManager(15);
shared_ptr<PosixThreadFactory> threadFactory
= shared_ptr<PosixThreadFactory>(new PosixThreadFactory());
threadManager->threadFactory(threadFactory);
threadManager->start();
printf("start.../n");
TThreadPoolServer server(processor,
serverTransport,
transportFactory,
protocolFactory,
threadManager);
server.serve();
printf("end/n");
return 0;
}
注意代码中的红色关键字,要将他们修改成你自己的service中定义的名字。
(2)头文件:
#include <concurrency/ThreadManager.h>
#include <concurrency/PosixThreadFactory.h>
#include <server/TThreadPoolServer.h>
#include <server/TThreadedServer.h>
能加的都加上吧,以免出现类似如下错误:
error: ‘ThreadManager’ was not declared in this scope
(3)命名空间:using namespace ::apache::thrift::concurrency;
否则出现错误:error: ‘PosixThreadFactory’ was not declared in this scope
concurrency是和#include中的文件目录是对应的
每一个客户端连接时,服务端都将启动一个线程为其服务,数据传输结束后,服务端回收这个线程。
- thrift多线程服务端
- thrift多线程服务端
- thrift多线程服务端
- thrift多线程服务端
- Thrift java 基于阻塞IO的服务端多线程通信
- Thrift服务端实现原理浅析
- Thrift python服务端出错 解决方法
- Thrift多线程误用惨案
- Thrift交流(二)thrift服务端和客户端实现 Nifty
- Thrift交流(二)thrift服务端和客户端实现 Nifty
- Thrift交流(二)thrift服务端和客户端实现 Nifty
- thrift JAVA服务端 python客户端的实现
- java实现Thrift服务端和客户端
- Thrift python服务端启动报错
- thrift服务端获取客户端ip地址。
- Android平台Thrift服务端开发总结
- thrift服务端的c++语言实现
- Thrift应用:Windows下C#服务端实现
- cookie session
- 悬挂指针
- 《#诗盗#·碉堡的机机》
- android intent和intent action大全
- java进度条
- thrift多线程服务端
- xp下修改hosts不起作用
- 忆往昔,总是让人潸然泪下
- Image转换成Mysql的blob类型
- oracle网络配置
- thrift三种server方式说明
- 标准C++ STL容器
- 4KB Alignment: How does it affect the efficiency of a hard disk
- ubuntu上卸载openjdk并安装sunjdk(转)