我正在学习如何使用 Libevent。虽然我无法理解挂起和活动之间的区别。在我看来,当一个事件被添加到 event_base 并且该事件还没有发生时,它处于挂起状态,而调用者等待的事件发生了,然后处于活动状态,对吗?但是,当我阅读描述时event_pending
,看到代码爆炸,它说当事件挂起时,改变内部状态是不好的它,我认为这里的“待定”一词是误解,它应该是“event_active”....我错了吗?
#include <event2/event.h>
#include <stdio.h>
/* Change the callback and callback_arg of 'ev', which must not be
* pending. */
int replace_callback(struct event *ev, event_callback_fn new_callback,
void *new_callback_arg)
{
struct event_base *base;
evutil_socket_t fd;
short events;
int pending;
pending = event_pending(ev, EV_READ|EV_WRITE|EV_SIGNAL|EV_TIMEOUT,
NULL);
if (pending) {
/* We want to catch this here so that we do not re-assign a
* pending event. That would be very very bad. */
fprintf(stderr,
"Error! replace_callback called on a pending event!\n");
return -1;
}
event_get_assignment(ev, &base, &fd, &events,
NULL /* ignore old callback */ ,
NULL /* ignore old callback argument */);
event_assign(ev, base, fd, events, new_callback, new_callback_arg);
return 0;
}