Libevent中待处理和活动事件之间的区别是什么?

时间:2012-09-12 14:55:00

标签: sockets network-programming libevent

我正在学习如何使用Libevent.While我无法理解pending和active之间的区别。在我看来,当一个事件被添加到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;
 }

2 个答案:

答案 0 :(得分:3)

挂起事件意味着触发了一些操作并且事件回调的执行在event_base中排队。

活动事件意味着事件回调当前正在执行当前线程或其他一些(不要忘记你可以操纵来自其他线程的事件,即没有运行事件循环)

重新分配挂起事件不是线程安全操作,您可以在event.c中看到它。

我认为如果您编写单线程应用程序,可以随时重新分配事件。

答案 1 :(得分:2)

libevent book quote(http://www.wangafu.net/~nickm/libevent-book/Ref4_event.html):

  

一旦调用Libevent函数来设置事件并将其与事件库关联,它就会被初始化。此时,您可以添加,这使其在基础中处于待处理状态。当事件处于挂起状态时,如果发生触发事件的条件(例如,其文件描述符更改状态或其超时到期),则事件变为活动状态,并且运行其(用户提供的)回调函数。

所以&#34;等待&#34;在复兴方面意味着&#34;只是加入反应堆&#34;。没有动作触发需要获得待处理事件。我用简单的程序检查了这个行为:

#include <stdio.h>
#include <unistd.h>
#include <assert.h>
#include <event.h>

void read_cb( int sock, short which, void *arg )
{
    printf( "read_cb() called\n" );
    fflush( stdout );
    // while our callback is running event is active
}

int main()
{
    struct event_base *base = event_base_new();

    int fd[ 2 ];
    assert( pipe( fd ) == 0 );

    struct event ev;
    event_set( & ev, fd[ 0 ], EV_READ | EV_PERSIST, read_cb, NULL );
    event_base_set( base, & ev );

    assert( event_add( & ev, NULL ) != -1 );
    // it's pending now, just after adding

    printf( "event_pending( ... ) == %d\n", event_pending( & ev, EV_READ, NULL ) );
    fflush( stdout );

    event_base_loop( base, EVLOOP_ONCE );

    return 0;
}

输出:

  

event_pending(...)== 2