ToB企服应用市场:ToB评测及商务社交产业平台

标题: libevent之event_base [打印本页]

作者: 火影    时间: 2024-6-26 13:16
标题: libevent之event_base
目次

创建event_base

在使用任何有趣的 Libevent 函数之前,您需要分配 一种或多种event_base布局。每个event_base布局都包含一组 事件,并可以轮询以确定哪些事件处于活动状态。
假如event_base设置为使用锁,则可以安全地访问它 多个线程。但是,它的循环只能在单个线程中运行。假如 您希望有多个线程轮询 IO,您需要有一个 每个线程event_base。
提示[Libevent 的未来版本大概支持运行event_bases 跨多个线程的事件。每个event_base都有一个“方法”,或者一个后端,它用来确定哪个 活动已预备就绪。公认的方法有:
用户可以使用环境变量禁用特定的后端。假如您想关闭 kqueue 后端,可以设置 EVENT _ NOKQUEUE 环境变量,以此类推。假如希望从步伐内部关闭后端,请参阅下面的 event _ config _ void _ method ()阐明。
设置默认event_base

event_base_new() 函数分配并返回一个新的事件库 默认设置。它查抄环境变量并返回 指向新event_base的指针。假如出现错误,则返回 NULL。
在方法之间进行选择时,它会选择操作系统最快的方法 支持。
接口
  1. struct event_base *event_base_new(void);
复制代码
对于大多数步伐,这就是您所需要的。
event_base_new() 函数在  中声明。它首先 出现在 Libevent 1.4.3 中。
设置复杂的event_base

假如你想更好地控制你得到什么样的event_base,你需要 使用event_config。event_config是一种不透明的布局,可以容纳 有关您对event_base偏好的信息。当你想要一个 event_base,将event_config传递给 event_base_new_with_config()。
接口
  1. struct event_config *event_config_new(void);
  2. struct event_base *event_base_new_with_config(const struct event_config *cfg);
  3. void event_config_free(struct event_config *cfg);
复制代码
若要分配具有这些函数的event_base,请调用 event_config_new() 以分配新event_config。然后,您在 event_config告诉它您的需求。最后,你可以调用 event_base_new_with_config() 获取新event_base。完成后, 您可以使用 event_config_free() 释放event_config。
接口
  1. int event_config_avoid_method(struct event_config *cfg, const char *method);
  2. enum event_method_feature {
  3.     EV_FEATURE_ET = 0x01,
  4.     EV_FEATURE_O1 = 0x02,
  5.     EV_FEATURE_FDS = 0x04,
  6. };
  7. int event_config_require_features(struct event_config *cfg,
  8.                                   enum event_method_feature feature);
  9. enum event_base_config_flag {
  10.     EVENT_BASE_FLAG_NOLOCK = 0x01,
  11.     EVENT_BASE_FLAG_IGNORE_ENV = 0x02,
  12.     EVENT_BASE_FLAG_STARTUP_IOCP = 0x04,
  13.     EVENT_BASE_FLAG_NO_CACHE_TIME = 0x08,
  14.     EVENT_BASE_FLAG_EPOLL_USE_CHANGELIST = 0x10,
  15.     EVENT_BASE_FLAG_PRECISE_TIMER = 0x20
  16. };
  17. int event_config_set_flag(struct event_config *cfg,
  18.     enum event_base_config_flag flag);
复制代码
调用 event_config_avoid_method 告诉 Libevent 避免特定的 按名称提供后端。调用 event_config_require_feature() 告诉 Libevent 不使用任何不能提供所有功能的后端。 调用 event_config_set_flag() 告诉 Libevent 设置一个或多个 构造事件库时下面的运行时标志。
event_config_require_features的公认特征值为:
event_config_set_flag() 的可识别选项值为:
上述操作event_config的函数在乐成时都返回 0, -1 失败时。
注意设置一个需要后端的event_config很轻易,您的 操作系统不提供。例如,从 Libevent 2.0.1-alpha 开始,没有 O(1) 用于 Windows 的后端,而 Linux 上没有同时提供这两种功能的后端 EV_FEATURE_FDS和EV_FEATURE_O1。假如您已进行配置 Libevent 无法满足,event_base_new_with_config() 将返回 NULL。接口
  1. int event_config_set_num_cpus_hint(struct event_config *cfg, int cpus)
复制代码
不过,此函数现在仅在使用 IOCP 时对 Windows 有用 将来它大概会对其他平台有用。调用它告诉 event_config,它产生的event_base应该只管好好利用 多线程时给定数量的 CPU。请注意,这只是一个提示: 事件库最终使用的 CPU 大概比您选择的更多或更少。
接口
  1. int event_config_set_max_dispatch_interval(struct event_config *cfg,
  2.     const struct timeval *max_interval, int max_callbacks,
  3.     int min_priority);
复制代码
此函数通过限制低优先级的数量来防止优先级倒置 在查抄更多高优先级事件之前,可以调用事件回调。 假如 max_interval 为非 null,则事件循环会查抄每个事件之后的时间 回调,假如已通过,则重新扫描高优先级事件max_interval。 假如max_callbacks为非负数,则事件循环还会查抄更多事件 调用max_callbacks回调后。这些规则适用于任何 min_priority 或更高的事件。
示例:首选边缘触发的后端
[code]struct event_config *cfg;struct event_base *base;int i;/* My program wants to use edge-triggered events if at all possible.  So   I'll try to get a base twice: Once insisting on edge-triggered IO, and   once not. */for (i=0; i




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4