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客户端组件提供一系列完善的配置项如连接超时 ...
随机推荐
- libcurl使用演示样例
简要说明:C++使用libcurl訪问"www.baidu.com".获取返回码和打印出http文件 /* * @ libcurl使用演示样例 * @ 2014.04.29 * @ ...
- 彻底理解Cisco/Linux/Windows的IP路由
-1.只要理解实质,名称并不重要! 很多使用Linux的网络高手在面对Cisco管理员的诸如管理距离,路由度量等词汇时,还没有PK就自觉败下阵来了.我觉得这实在太可惜了,大家本是一家,为何这么为难对方 ...
- [JavaScript] Array.prototype.reduce in JavaScript by example
Let's take a closer look at using Javascript's built in Array reduce function. Reduce is deceptively ...
- TCP 函数
[root@localhost tt]# man listen LISTEN() Linux Programmer’s Manual LISTEN() NAME listen - listen for ...
- Qt 学习之路 :信号槽
信号槽是 Qt 框架引以为豪的机制之一.熟练使用和理解信号槽,能够设计出解耦的非常漂亮的程序,有利于增强我们的技术设计能力. 所谓信号槽,实际就是观察者模式.当某个事件发生之后,比如,按钮检测到自己被 ...
- poj3348 Cows 凸包+多边形面积 水题
/* poj3348 Cows 凸包+多边形面积 水题 floor向下取整,返回的是double */ #include<stdio.h> #include<math.h> # ...
- Realm Configuration HOW-TO--官方
来源:https://secure.gettinglegaldone.com/docs/realm-howto.html Quick Start This document describes how ...
- Android read-only file system解决方法
adb shell su - mount -o rw,remount /system
- HTML5之填写个人信息
- (转)asp.net分页存储过程
Asp.Net分页存储过程 SQL分页语句 一.比较万能的分页: sql代码: 1 2 3 select top 每页显示的记录数 * from topic where id not in (sel ...