非阻塞模式

#include "RpcServiceHandler.h"

#include <thrift/concurrency/ThreadManager.h>
#include <thrift/concurrency/PosixThreadFactory.h>
#include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/server/TThreadPoolServer.h>
#include <thrift/server/TNonblockingServer.h>
#include <thrift/server/TThreadedServer.h>
#include <thrift/transport/TServerSocket.h>
#include <thrift/transport/TBufferTransports.h>
#include <thrift/TToString.h> int main(int argc, char **argv)
{
RpcServiceHandler *rpcServiceHanlder = new RpcServiceHandler(); int port = CFG()->getInt(kCfgProcPort);
int workerCount = CFG()->getInt(kCfgProcWCnt); boost::shared_ptr<RpcServiceHandler> handler(rpcServiceHanlder);
boost::shared_ptr<TProcessor> processor(new RpcServiceProcessor(handler));
boost::shared_ptr<TServerTransport> serverTransport(new TServerSocket(port));
boost::shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory()); boost::shared_ptr<ThreadManager> threadManager = ThreadManager::newSimpleThreadManager(workerCount);
boost::shared_ptr<PosixThreadFactory> threadFactory = boost::shared_ptr<PosixThreadFactory>(new PosixThreadFactory());
threadManager->threadFactory(threadFactory);
threadManager->start(); TNonblockingServer server(processor,
protocolFactory,
port,
threadManager); std::cout << "Starting the server..." << std::endl; server.serve();
return 0;
}

  

线程池模式

#include "RpcServiceHandler.h"

#include <thrift/concurrency/ThreadManager.h>
#include <thrift/concurrency/PosixThreadFactory.h>
#include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/server/TThreadPoolServer.h>
#include <thrift/server/TNonblockingServer.h>
#include <thrift/server/TThreadedServer.h>
#include <thrift/transport/TServerSocket.h>
#include <thrift/transport/TBufferTransports.h>
#include <thrift/TToString.h> int main(int argc, char **argv)
{
RpcServiceHandler *rpcServiceHanlder = new RpcServiceHandler(); int port = CFG()->getInt(kCfgProcPort);
int workerCount = CFG()->getInt(kCfgProcWCnt); boost::shared_ptr<RpcServiceHandler> handler(rpcServiceHanlder);
boost::shared_ptr<TProcessor> processor(new RpcServiceProcessor(handler));
boost::shared_ptr<TServerTransport> serverTransport(new TServerSocket(port));
boost::shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
boost::shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory()); boost::shared_ptr<ThreadManager> threadManager = ThreadManager::newSimpleThreadManager(workerCount);
boost::shared_ptr<PosixThreadFactory> threadFactory = boost::shared_ptr<PosixThreadFactory>(new PosixThreadFactory());
threadManager->threadFactory(threadFactory);
threadManager->start(); TThreadPoolServer server(processor,
serverTransport,
transportFactory,
protocolFactory,
threadManager); std::cout << "Starting the server..." << std::endl; server.serve();
return 0;
}

单独Server模式

#include "RpcServiceHandler.h"

#include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/server/TSimpleServer.h>
#include <thrift/transport/TServerSocket.h>
#include <thrift/transport/TBufferTransports.h>
#include <thrift/TToString.h> int main(int argc, char **argv)
{
RpcServiceHandler *rpcServiceHanlder = new RpcServiceHandler(); int port = CFG()->getInt(kCfgProcPort); boost::shared_ptr<RpcServiceHandler> handler(rpcServiceHanlder);
boost::shared_ptr<TProcessor> processor(new RpcServiceProcessor(handler));
boost::shared_ptr<TServerTransport> serverTransport(new TServerSocket(port));
boost::shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
boost::shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory()); std::cout << "Starting the server..." << std::endl;
TSimpleServer server(processor, serverTransport, transportFactory, protocolFactory); server.serve();
return 0;
}

  

Thrift的C++服务端(线程池和非阻塞)模式的更多相关文章

  1. 将服务端select设置为非阻塞,处理更多业务

    服务端代码: #include<WinSock2.h> #include<Windows.h> #include<vector> #include<stdio ...

  2. Python中的Tcp协议应用之TCP服务端-线程版

    利用线程实现,一个服务端同时服务多个客户端的需求. TCP服务端-线程版代码实现: import socket import threading def handle_client_socket(ne ...

  3. Java线程池(Callable+Future模式)

    转: Java线程池(Callable+Future模式) Java线程池(Callable+Future模式) Java通过Executors提供四种线程池 1)newCachedThreadPoo ...

  4. springBoot服务整合线程池ThreadPoolTaskExecutor与@Async详解使用

    ThreadPoolExecutor:=======这个是java自己实现的线程池执行类,基本上创建线程池都是通过这个类进行的创建.ThreadPoolTaskExecutor:========这个是 ...

  5. 设计模式:基于线程池的并发Visitor模式

    1.前言 第二篇设计模式的文章我们谈谈Visitor模式. 当然,不是简单的列个的demo,我们以电商网站中的购物车功能为背景,使用线程池实现并发的Visitor模式,并聊聊其中的几个关键点. 一,基 ...

  6. spring线程池ThreadPoolTaskExecutor与阻塞队列BlockingQueue

    一: ThreadPoolTaskExecutor是一个spring的线程池技术,查看代码可以看到这样一个字段: private ThreadPoolExecutor threadPoolExecut ...

  7. java多线程:线程池原理、阻塞队列

    一.线程池定义和使用 jdk 1.5 之后就引入了线程池. 1.1 定义 从上面的空间切换看得出来,线程是稀缺资源,它的创建与销毁是一个相对偏重且耗资源的操作,而Java线程依赖于内核线程,创建线程需 ...

  8. 线程池ThreadPoolExecutor与阻塞队列BlockingQueue应用

    1.线程池介绍 JDK5.0以上: java.util.concurrent.ThreadPoolExecutor  构造函数签名: public ThreadPoolExecutor( int co ...

  9. ThreadPool.QueueUserWorkItem引发的血案,线程池异步非正确姿势导致程序闪退的问题

    ThreadPool是.net System.Threading命名空间下的线程池对象.使用QueueUserWorkItem实现对异步委托的先进先出有序的回调.如果在回调的方法里面发生异常则应用程序 ...

随机推荐

  1. 【ABP框架系列学习】模块系统(4)之插件示例开发

    0.引言 上一篇博文主要介绍了ABP模块及插件的相关知识,本章节主要开发一个插件示例来学习如何创建一个插件,并在应用程序中使用.这个命名为FirstABPPlugin的插件主要在指定的时间段内删除审计 ...

  2. mysql 开发进阶篇系列 55 权限与安全(安全事项 )

    一. 操作系统层面安全 对于数据库来说,安全很重要,本章将从操作系统和数据库两个层面对mysql的安全问题进行了解. 1. 严格控制操作系统账号和权限 在数据库服务器上要严格控制操作系统的账号和权限, ...

  3. Jenkins技巧:如何启动、停止、重启、重载Jenkins

    ----------------------------------------------------------------- 原创博文,如需转载请通知作者并注明出处! 博主:疲惫的豆豆 链接:h ...

  4. [Shell]sed命令在MAC和Linux下的不同使用方式

    ---------------------------------------------------------------------------------------------------- ...

  5. 通过keras例子理解LSTM 循环神经网络(RNN)

    博文的翻译和实践: Understanding Stateful LSTM Recurrent Neural Networks in Python with Keras 正文 一个强大而流行的循环神经 ...

  6. IFS简单说明

    bash&shell系列文章:http://www.cnblogs.com/f-ck-need-u/p/7048359.html bash下的很多命令都会分割单词,绝大多数时候默认是采用空格作 ...

  7. vim之添加多行和删除多行

    1.复制单行和多行. 1)单行复制 在命令模式下,将光标移到将要复制的行处,按“yy”进行复制,按“p”进行粘贴. 2)多行复制 在命令模式下,将光标移到将要复制的行处,按“nyy”进行复制(n代表行 ...

  8. MySQL系列详解二:MySQL语句操作-技术流ken

    简介 本篇博客将详细讲解mysql的一些常用sql语句操作,例如创建数据库,删除数据库,创建表,修改表,删除表,以及简单查询案例. 关于mysql数据中的SQL的大小写问题 1.不区分大小写 1. s ...

  9. SpringBoot学习(六)-->SpringBoot的自动配置的原理

    Spring Boot的自动配置的原理 Spring Boot在进行SpringApplication对象实例化时会加载META-INF/spring.factories文件,将该配置文件中的配置载入 ...

  10. iframe关闭操作

    关闭自定义 Div+Iframe弹窗 :window.parent.$("div的id/class/name").remove();//移除div 关闭Iframe弹窗:windo ...