libevent学习文档(二)eventbase相关接口和参数
Setting up a default event_base
The event_base_new() function allocates and returns a new event base with the default settings. It examines the environment variables and returns a pointer to a new event_base.
If there is an error, it returns NULL.
When choosing among methods, it picks the fastest method that the OS supports.
event_base_new()创建默认event_base,他检测环境变量并且返回一个指向新的event_base的指针。
当调用方法时,选择操作系统支持的做快速的方法。
Interface
struct event_base *event_base_new(void);
Setting up a complicated event_base
If you want more control over what kind of event_base you get, you need to use an event_config. An event_config is an opaque structure that holds information about your preferences for an event_base. When you want an event_base, you pass the event_config to event_base_new_with_config().
想要创建指定类型的event_base,需要使用event_config,event_config是不透明的结构体,保存了您创建event_base时需要的特性。
用户调用event_base_new_with_config创建特殊类型的event_base
Interface
struct event_config *event_config_new(void);
struct event_base *event_base_new_with_config(const struct event_config *cfg);
void event_config_free(struct event_config *cfg);
To allocate an event_base with these functions, you call event_config_new() to allocate a new event_config. Then, you call other functions on the event_config to tell it about your needs. Finally, you call event_base_new_with_config() to get a new event_base. When you are done, you can free the event_config with event_config_free().
为了创造一个有特定功能的event_base,你需要调用event_config_new()创建event_config,只后调用event_base_new_with_config(),之后调用event_config_free释放。
Interface:
int event_config_avoid_method(struct event_config *cfg, const char *method);
enum event_method_feature {
EV_FEATURE_ET = 0x01,
EV_FEATURE_O1 = 0x02,
EV_FEATURE_FDS = 0x04,
};
int event_config_require_features(struct event_config *cfg,
enum event_method_feature feature);
enum event_base_config_flag {
EVENT_BASE_FLAG_NOLOCK = 0x01,
EVENT_BASE_FLAG_IGNORE_ENV = 0x02,
EVENT_BASE_FLAG_STARTUP_IOCP = 0x04,
EVENT_BASE_FLAG_NO_CACHE_TIME = 0x08,
EVENT_BASE_FLAG_EPOLL_USE_CHANGELIST = 0x10,
EVENT_BASE_FLAG_PRECISE_TIMER = 0x20
};
int event_config_set_flag(struct event_config *cfg,
enum event_base_config_flag flag);
Calling event_config_avoid_method tells Libevent to avoid a specific available backend by name. Calling event_config_require_feature() tells Libevent not to use any backend that cannot supply all of a set of features. Calling event_config_set_flag() tells Libevent to set one or more of the run-time flags below when constructing the event base.
event_config_avoid_method 可以规避一些函数,通过传入函数的名字,event_config_require_feature告诉libevent不要使用不支持指定特性的功能。
event_config_set_flag告诉libevent设置一个或多个运行时标记,当你创建event base时。
特性的标记位如下:
EV_FEATURE_ET:要求后端方法支持edge-triggered IO
EV_FEATURE_O1:要求添加或者删除,或者激活某个事件的事件复杂度为O(1)
EV_FEATURE_FDS:要求后端方法支持任意类型的文件描述符,不仅仅是socket
EVENT_BASE_FLAG_NOLOCK:不为event_base设置锁,可以省去event_base加锁和释放锁的时间,但是对于多线程操作,并不安全。
EVENT_BASE_FLAG_IGNORE_ENV:当调用后端的方法时并不检查EVENT_*环境变量,用这个选项时注意,会导致程序和Libevent库之间调试麻烦
EVENT_BASE_FLAG_STARTUP_IOCP:仅对windows有效,通知Libevent在启动的时候就是用必要的IOCP 派发逻辑,而不是在需要时才用IOCP派发逻辑。
EVENT_BASE_FLAG_NO_CACHE_TIME:每次超时回调函数调用后检测当前时间,而不是准备调用超时回调函数前检测。
EVENT_BASE_FLAG_EPOLL_USE_CHANGELIST:通知libevent,如果后台使用epoll模型,使用changelist是安全的,
epoll-changelist 能够避免在两次派发之间如果同一个fd的状态有改变,多次不必要的系统调用。换句话说在派发消息中间,如果同一个fd多次改动,那么最终只调用一次系统
调用。如果后台不是epoll模型,那么这个选项是没什么影响。同样可以设置EVENT_EPOLL_USE_CHANGELIST 环境变量达到这个目的。
EVENT_BASE_FLAG_PRECISE_TIMER:libevent默认使用快速的定时器机制,设置这个选项后,如果有一个慢速的但是定时器精度高的机制,那么就会切换到这个机制。
如果没有这个机制,那么这个选项没什么影响。
下面几个例子说明了这些api用法
static void
test_base_features(void *arg)
{
struct event_base *base = NULL;
struct event_config *cfg = NULL;
//创建一个event_config
cfg = event_config_new();
//设置在epoll模型情况下支持et模式
tt_assert( == event_config_require_features(cfg, EV_FEATURE_ET));
//创建event_base
base = event_base_new_with_config(cfg);
if (base) {
tt_int_op(EV_FEATURE_ET, ==,
event_base_get_features(base) & EV_FEATURE_ET);
} else {
base = event_base_new();
tt_int_op(, ==, event_base_get_features(base) & EV_FEATURE_ET);
} end:
//结束时要释放base和config
if (base)
event_base_free(base);
if (cfg)
event_config_free(cfg);
}
下面这个是测试方法的
static void
test_methods(void *ptr)
{
//获取event_base支持的方法
const char **methods = event_get_supported_methods();
struct event_config *cfg = NULL;
struct event_base *base = NULL;
const char *backend;
int n_methods = ; tt_assert(methods); backend = methods[];
while (*methods != NULL) {
TT_BLATHER(("Support method: %s", *methods));
++methods;
++n_methods;
} cfg = event_config_new();
assert(cfg != NULL);
//event_base屏蔽backend的方法
tt_int_op(event_config_avoid_method(cfg, backend), ==, );
//并且设置了忽略ENV环境变量的选项
event_config_set_flag(cfg, EVENT_BASE_FLAG_IGNORE_ENV);
//创建event_base
base = event_base_new_with_config(cfg);
if (n_methods > ) {
tt_assert(base);
tt_str_op(backend, !=, event_base_get_method(base));
} else {
tt_assert(base == NULL);
} end:
if (base)
event_base_free(base);
if (cfg)
event_config_free(cfg);
}
我的公众号:

libevent学习文档(二)eventbase相关接口和参数的更多相关文章
- libevent学习文档(三)working with event
Events have similar lifecycles. Once you call a Libevent function to set up an event and associate i ...
- 2013 最新的 play web framework 版本 1.2.3 框架学习文档整理
Play framework框架学习文档 Play framework框架学习文档 1 一.什么是Playframework 3 二.playframework框架的优点 4 三.Play Frame ...
- soapUI学习文档(转载)
soapUI 学习文档不是前言的前言记得一个搞开发的同事突然跑来叫能不能做个WebService 性能测试,当时我就凌乱了,不淡定啊,因为我是做测试的,以前连WebService 是什么不知道,毕竟咱 ...
- NodeJS-001-Nodejs学习文档整理(转-出自http://www.cnblogs.com/xucheng)
Nodejs学习文档整理 http://www.cnblogs.com/xucheng/p/3988835.html 1.nodejs是什么: nodejs是一个是javascript能在后台运行的平 ...
- Openstack api 学习文档 & restclient使用文档
Openstack api 学习文档 & restclient使用文档 转载请注明http://www.cnblogs.com/juandx/p/4943409.html 这篇文档总结一下我初 ...
- Openstack python api 学习文档 api创建虚拟机
Openstack python api 学习文档 转载请注明http://www.cnblogs.com/juandx/p/4953191.html 因为需要学习使用api接口调用openstack ...
- 操作PDF文档功能的相关开源项目探索——iTextSharp 和PDFBox
原文 操作PDF文档功能的相关开源项目探索——iTextSharp 和PDFBox 很久没自己写写心得日志与大家分享了,一方面是自己有点忙,一方面是自己有点懒,没有及时总结.因为实践是经验的来源,总结 ...
- Ext JS 6学习文档-第8章-主题和响应式设计
Ext JS 6学习文档-第8章-主题和响应式设计 主题和响应式设计 本章重点在 ExtJS 应用的主题和响应式设计.主要有以下几点内容: SASS 介绍和入门 主题 响应式设计 SASS 介绍和入门 ...
- Ext JS 6学习文档-第3章-基础组件
Ext JS 6学习文档-第3章-基础组件 基础组件 在本章中,你将学习到一些 Ext JS 基础组件的使用.同时我们会结合所学创建一个小项目.这一章我们将学习以下知识点: 熟悉基本的组件 – 按钮, ...
随机推荐
- 减少Java垃圾的产生,降低内存使用量
1.尽量少使用静态的变量,因为它会一直占用内存, 2.尽量少使用String字符串去做拼接,相加.因为String是定长的每次相加都会产生新的临时对象,生成垃圾对象,尽量使用StringBuffer, ...
- openstack-r版(rocky)搭建基于centos7.4 的openstack swift对象存储服务 二
openstack-r版(rocky)搭建基于centos7.4 的openstack swift对象存储服务 一 openstack-r版(rocky)搭建基于centos7.4 的openstac ...
- 排查GCC 4.4.X版本优化switch-enum的BUG
起因 一次偶然碰到一个诡异的bug,现象是同一份C++代码使用GCC4.4.x版本在开启优化前和优化后的结果不一样,优化后的代码逻辑不正确. 示例代码如下: //main.cpp #include & ...
- 天马行空-Ops平台建设概述
1 概述 什么是Ops平台,Ops平台的目标是什么,建设的考虑点有哪些?本章节以实际生活中医院的例子来进行各形象的阐述. 医院包含各种诊断治疗设备,病历库,医生.一个孕妇需要到医院 ...
- Python操作摄像头
实践环境: 操作系统:Windows 7(X64) Python版本:python-2.7.13.msi 使用插件:pygame-1.9.1.win32-py2.7.msi 软件下载: python- ...
- mysql innodb myisam 比较
InnoDB: 支持事务处理等 不加锁读取 支持外键 支持行锁 不支持FULLTEXT类型的索引 不保存表的具体行数,扫描表来计算有多少行 DELETE 表时,是一行一行的删除 InnoDB 把数据和 ...
- wepy中如何使用stylus等样式预处理器
wepy中如何使用stylus等样式预处理器 一.如何在wepy中使用stylus 1.安装wepy-compiler-stylus(以及stylus, stylus-loader) npm inst ...
- Phonegap 环境配置
目前要开发 Web App 还是有比较多的选择的 如 Phonegap.MUI.AppCan,接下来以 Web前端开发工程师 的角度来一个 Phonegap 的 First Blood 一.开发环境: ...
- Nginx连载
一. nginx变量(用户变量.内建变量) 用户变量 又称用户自定义变量 Nginx用户变量的可见范围是整个配置文件,甚至可以跨越不通虚拟主机的server配置,但是变量适用范围是不可以跨越自己的容器 ...
- Python Requests库入门——应用实例-京东商品页面爬取+模拟浏览器爬取信息
京东商品页面爬取 选择了一款荣耀手机的页面(给华为打广告了,荣耀play真心不错) import requests url = "https://item.jd.com/7479912.ht ...