libevent带负载均衡的多线程使用示例
- eventtest : eventtest.c
- gcc -Wall -g -levent -lpthread -o eventtest eventtest.c
- .PHONY : clean
- clean :
- rm eventtest -f
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <pthread.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <event.h>
- typedef struct {
- pthread_t tid;
- struct event_base *base;
- struct event event;
- int read_fd;
- int write_fd;
- }LIBEVENT_THREAD;
- typedef struct {
- pthread_t tid;
- struct event_base *base;
- }DISPATCHER_THREAD;
- const int thread_num = 10;
- LIBEVENT_THREAD *threads;
- DISPATCHER_THREAD dispatcher_thread;
- int last_thread = 0;
- static void
- thread_libevent_process(int fd, short which, void *arg)
- {
- int ret;
- char buf[128];
- LIBEVENT_THREAD *me = arg;
- if (fd != me->read_fd) {
- printf("thread_libevent_process error : fd != me->read_fd\n");
- exit(1);
- }
- ret = read(fd, buf, 128);
- if (ret > 0) {
- buf[ret] = '\0';
- printf("thread %llu receive message : %s\n", (unsigned long long)me->tid, buf);
- }
- return;
- }
- static void *
- worker_thread(void *arg)
- {
- LIBEVENT_THREAD *me = arg;
- me->tid = pthread_self();
- event_base_loop(me->base, 0);
- return NULL;
- }
- static void
- timeout_cb(int fd, short event, void *arg)
- {
- struct timeval tv;
- struct event *timeout = arg;
- int tid = (last_thread + 1) % thread_num; //memcached中线程负载均衡算法
- LIBEVENT_THREAD *thread = threads + tid;
- last_thread = tid;
- write(thread->write_fd, "Hello world!", sizeof("Hello world!") - 1);
- evutil_timerclear(&tv);
- tv.tv_sec = 1;
- event_add(timeout, &tv);
- }
- int
- main (int argc, char *argv[])
- {
- int ret;
- int i;
- int fd[2];
- struct event timeout;
- struct timeval tv;
- pthread_t tid;
- dispatcher_thread.base = event_init();
- if (dispatcher_thread.base == NULL) {
- perror("event_init( base )");
- return 1;
- }
- dispatcher_thread.tid = pthread_self();
- threads = calloc(thread_num, sizeof(LIBEVENT_THREAD));
- if (threads == NULL) {
- perror("calloc");
- return 1;
- }
- for (i = 0; i < thread_num; i++) {
- ret = socketpair(AF_LOCAL, SOCK_STREAM, 0, fd);
- if (ret == -1) {
- perror("socketpair()");
- return 1;
- }
- threads[i].read_fd = fd[1];
- threads[i].write_fd = fd[0];
- threads[i].base = event_init();
- if (threads[i].base == NULL) {
- perror("event_init()");
- return 1;
- }
- event_set(&threads[i].event, threads[i].read_fd, EV_READ | EV_PERSIST, thread_libevent_process, &threads[i]);
- event_base_set(threads[i].base, &threads[i].event);
- if (event_add(&threads[i].event, 0) == -1) {
- perror("event_add()");
- return 1;
- }
- }
- for (i = 0; i < thread_num; i++) {
- pthread_create(&tid, NULL, worker_thread, &threads[i]);
- }
- evtimer_set(&timeout, timeout_cb, &timeout);
- event_base_set(dispatcher_thread.base, &timeout);
- evutil_timerclear(&tv);
- tv.tv_sec = 1;
- event_add(&timeout, &tv);
- event_base_loop(dispatcher_thread.base, 0);
- return 0;
- }
libevent带负载均衡的多线程使用示例的更多相关文章
- Ribbon自带负载均衡策略比较
Ribbon自带负载均衡策略比较 策略名 策略声明 策略描述 实现说明 BestAvailableRule public class BestAvailableRule extends ClientC ...
- Ribbon自带负载均衡策略
IRule这是所有负载均衡策略的父接口,里边的核心方法就是choose方法,用来选择一个服务实例. AbstractLoadBalancerRuleAbstractLoadBalancerRule是一 ...
- HAProxy(二):HAProxy的ACL规则实现智能负载均衡详解与示例
一.HAProxy的ACL的功能 ACL(Access Control List)访问控制列表,HAProxy中的ACL的匹配条件和控制条件有许多种,功能很强大,可以通过源地址.源端口.目标地址.目标 ...
- apache、mod_jk负载均衡与tomcat集群
最近需要搭建apache和tomcat的集群,实现静态网站直接通过apache访问,动态网站转交给tomcat处理,实现负载均衡和tomcat集群配置. apache安装 wget http://ap ...
- nginx负载均衡(5种方式)、rewrite重写规则及多server反代配置梳理
Nginx除了可以用作web服务器外,他还可以用来做高性能的反向代理服务器,它能提供稳定高效的负载均衡解决方案.nginx可以用轮询.IP哈希.URL哈希等方式调度后端服务器,同时也能提供健康检查功能 ...
- 客户端负载均衡Ribbon之一:Spring Cloud Netflix负载均衡组件Ribbon介绍
Netflix:['netfliːks] ribbon:英[ˈrɪbən]美[ˈrɪbən]n. 带; 绶带; (打印机的) 色带; 带状物;v. 把…撕成条带; 用缎带装饰; 形成带状; L ...
- Dubbo学习(二) Dubbo 集群容错模式-负载均衡模式
Dubbo是Alibaba开源的分布式服务框架,我们可以非常容易地通过Dubbo来构建分布式服务,并根据自己实际业务应用场景来选择合适的集群容错模式,这个对于很多应用都是迫切希望的,只需要通过简单的配 ...
- (转)nginx负载均衡(5种方式)、rewrite重写规则及多server反代配置梳理
Nginx除了可以用作web服务器外,他还可以用来做高性能的反向代理服务器,它能提供稳定高效的负载均衡解决方案.nginx可以用轮询.IP哈希.URL哈希等方式调度后端服务器,同时也能提供健康检查功能 ...
- spring-cloud-starter-ribbon提供客户端的软件负载均衡算法
Ribbon是什么? Ribbon是Netflix发布的开源项目,主要功能是提供客户端的软件负载均衡算法,将Netflix的中间层服务连接在一起.Ribbon客户端组件提供一系列完善的配置项如连接超时 ...
随机推荐
- 树莓派做AP发射wifi(RTL8188CUS芯片) 分类: shell ubuntu Raspberry Pi 2014-11-29 01:25 822人阅读 评论(0) 收藏
最近在做一个项目,需要用树莓派作为AP发射wifi,对比cubieboard,树莓派的配置容易得多,而且支持也更多. 较为官方的介绍配置为无线热点的文章莫过于这一篇<RPI-Wireless-H ...
- hadoop编程技巧(4)---总体情况key按类别搜索TotalOrderPartitioner
Hadoop代码测试版:Hadoop2.4 原理:携带MR该程序随机抽样提取前的输入数据,样本分类,然后,MR该过程的中间Partition此值用于当样品排序分组数据.这使得可以实现全球排名的目的. ...
- AS【常用插件】
安装插件,Settings -->[Plugins]-->搜索-->点击install-->重启AS 禁用插件,右侧面板会显示出已经安装的插件列表,取消勾选即可禁用插件 AS插 ...
- linux创建用户
创建用户 sudo adduser xxx 删除用户 sudo userdel xxx 删除用户和目录 sudo userdel -r xxx
- Xcode 常用编译选项设置
Xcode 常用编译选项设置 在xcconfig文件中指定即可. 用标准库连接 LINK_WITH_STANDARD_LIBRARIES = YES如果激活此设置,那么编译器在链接过程中会自动使用通过 ...
- angular template浅析
在我们浏览的页面中有大的网站,也有中小型网站,类型不同其中的页面也就不同,但是纵观大部分的网页是否有什么相同的地方呢?如果浏览的是一般的门户网站或者是什么小型的页面的话这种感觉就不是很明显,但是如果关 ...
- ios开发之xcode6中如何添加pch全局引用文件
xcode6中去掉了默认添加pch文件,这就需要我们自己手动添加pch文件了,添加pch文件是为了一些琐碎的头文件引用,加快编译速度! 下面就说下该如何手动添加pch文件: 1.添加一个文件,在oth ...
- Bootstrap_表单_按钮
一.多标签支持 一般制作按钮除了使用<button>标签元素之外,还可以使用<input type="submit">和<a>标签等. 同样,在 ...
- 固定DIV样式
<!doctype html> <html> <head> <meta charset="UTF-8"> < ...
- An erroroccurred while filtering resources
maven报错: maven An error occurred while filtering resources Maven -> Update Project... resolved th ...