libevent 学习

libevent 学习一 主要结构体

主要结构体

struct event_base{
/** Function pointers and other data to describe this event_base's
 * backend. */
**const struct eventop *evsel;**
/** Pointer to backend-specific data. */
**void *evbase;**
/** List of changes to tell backend about at next dispatch.  Only used
 * by the O(1) backends. */
struct event_changelist changelist;

/** Function pointers used to describe the backend that this event_base
 * uses for signals */
const struct eventop *evsigsel;
/** Data to implement the common signal handelr code. */
struct evsig_info sig;

/** Number of virtual events */
int virtual_event_count;
/** Number of total events added to this event_base */
int event_count;
/** Number of total events active in this event_base */
int event_count_active;

/** Set if we should terminate the loop once we're done processing
 * events. */
int event_gotterm;
/** Set if we should terminate the loop immediately */
int event_break;
/** Set if we should start a new instance of the loop immediately. */
int event_continue;

/** The currently running priority of events */
int event_running_priority;

/** Set if we're running the event_base_loop function, to prevent
 * reentrant invocation. */
int running_loop;

/* Active event management. */
/** An array of nactivequeues queues for active events (ones that
 * have triggered, and whose callbacks need to be called).  Low
 * priority numbers are more important, and stall higher ones.
 */
struct event_list *activequeues;
/** The length of the activequeues array */
int nactivequeues;

/* common timeout logic */

/** An array of common_timeout_list* for all of the common timeout
 * values we know. */
struct common_timeout_list **common_timeout_queues;
/** The number of entries used in common_timeout_queues */
int n_common_timeouts;
/** The total size of common_timeout_queues. */
int n_common_timeouts_allocated;

/** List of defered_cb that are active.  We run these after the active
 * events. */
struct deferred_cb_queue defer_queue;

/** Mapping from file descriptors to enabled (added) events */
struct event_io_map io;

/** Mapping from signal numbers to enabled (added) events. */
struct event_signal_map sigmap;

/** All events that have been enabled (added) in this event_base */
struct event_list eventqueue;

/** Stored timeval; used to detect when time is running backwards. */
struct timeval event_tv;

/** Priority queue of events with timeouts. */
struct min_heap timeheap;

/** Stored timeval: used to avoid calling gettimeofday/clock_gettime
 * too often. */
struct timeval tv_cache;

#if defined(_EVENT_HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
/** Difference between internal time (maybe from clock_gettime) and
 * gettimeofday. */
struct timeval tv_clock_diff;
/** Second in which we last updated tv_clock_diff, in monotonic time. */
time_t last_updated_clock_diff;
#endif

#ifndef _EVENT_DISABLE_THREAD_SUPPORT
/* threading support */
/** The thread currently running the event_loop for this base */
unsigned long th_owner_id;
/** A lock to prevent conflicting accesses to this event_base */
void *th_base_lock;
/** The event whose callback is executing right now */
struct event *current_event;
/** A condition that gets signalled when we're done processing an
 * event with waiters on it. */
void *current_event_cond;
/** Number of threads blocking on current_event_cond. */
int current_event_waiters;
#endif

#ifdef WIN32
/** IOCP support structure, if IOCP is enabled. */
struct event_iocp_port *iocp;
#endif

/** Flags that this base was configured with */
enum event_base_config_flag flags;

/* Notify main thread to wake up break, etc. */
/** True if the base already has a pending notify, and we don't need
 * to add any more. */
int is_notify_pending;
/** A socketpair used by some th_notify functions to wake up the main
 * thread. */
evutil_socket_t th_notify_fd[2];
/** An event used by some th_notify functions to wake up the main
 * thread. */
struct event th_notify;
/** A function used to wake up the main thread from another thread. */
int (*th_notify_fn)(struct event_base *base);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于学习libevent,你可以按照以下步骤进行: 1. 了解libeventlibevent是一个开源的事件通知库,它提供了事件驱动的网络编程接口,可以用于开发高性能的网络服务器和客户端应用。它支持多种I/O模型(包括基于事件的和多线程的),并提供了跨平台的兼容性。 2. 安装libevent:你可以从libevent的官方网站(https://libevent.org/)上下载最新版本的libevent,并按照官方文档中的指南进行安装。根据你使用的操作系统不同,安装步骤可能会有所不同。 3. 学习libevent的基本概念:了解libevent中的一些核心概念,如事件循环(event loop)、事件处理器(event handler)、事件回调函数(event callback)等。理解这些概念对于正确使用libevent非常重要。 4. 掌握libevent的使用方法:学习如何使用libevent来编写网络应用程序。这包括创建事件循环、注册事件、定义事件回调函数等。libevent提供了丰富的API,你可以根据自己的需求选择合适的接口进行开发。 5. 深入研究libevent的高级特性:学习libevent的更高级功能,如定时器、信号处理、缓冲区管理等。这些功能可以帮助你更好地控制和优化你的网络应用。 6. 查阅文档和示例代码:libevent的官方网站提供了详细的文档和示例代码,你可以利用这些资源来加深对libevent的理解。此外,还可以参考一些开源项目中使用libevent的实际案例,以便更好地应用于自己的项目中。 记住,学习任何新的库或工具都需要有耐心和实践。不断尝试和练习,结合实际项目,才能更好地掌握和应用libevent。祝你学习顺利!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值