简体   繁体   中英

libevent - event_base_loop() should it get events repeatly?

Here is a simple program using libevent on linux, it tracks the stdout fd, when it's writable, the callback will print some info to stdout .


Code

hello_libevent.c:

// libevent hello
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>

#include <event2/event.h>
#include <event2/thread.h>


void event_callback(evutil_socket_t fd, short events, void *arg) {
    if(events | EV_WRITE) {
        write(fd, "hello\n", 7);
    }

    sleep(1);
}

int libevent_test() {
    int opr;

    // enable pthread
    if(evthread_use_pthreads() == -1) {
        printf("error while evthread_use_pthreads(): %s\n", strerror(errno));
        return -1;
    }

    // create event_base
    struct event_base* eb;
    if((eb = event_base_new()) == NULL) {
        printf("error while event_base_new(): %s\n", strerror(errno));
        return -1;
    }

    // create event
    int fd_stdout = fileno(stdout);
    struct event* event_stdout;
    event_stdout = event_new(eb, fd_stdout, EV_WRITE, &event_callback, NULL);

    // add event as pending
    struct timeval timeout = {10, 0};
    if(event_add(event_stdout, &timeout) == -1) {
        printf("error while event_add(): %s\n", strerror(errno));
        return -1;
    }

    // dispatch
    if((opr = event_base_loop(eb, EVLOOP_NONBLOCK)) == -1) {
        printf("error while event_base_dispatch(): %s\n", strerror(errno));
        return -1;
    } else if(opr == 1) {
        printf("no more events\n");
    } else {
        printf("exit normally\n");
    }

    // free event
    event_free(event_stdout);

    return 0;
}

int main(int argc, char * argv[]) {
    return libevent_test();
}

Compile:

gcc -Wall hello_libevent.c -levent -levent_pthreads

Execution result:

hello
no more events

Questions:

  • In the test, event only occur once, is that the expected behavior? Or it should loop to get more event until timeout?
  • How to make it get event continuously? Is it necessary to call event_base_loop within a loop, while it's already a loop ?

From http://www.wangafu.net/~nickm/libevent-book/Ref3_eventloop.html it looks like you can call event_base_loop or event_base_dispatch from within a loop.

while (1) { /* This schedules an exit ten seconds from now. */ event_base_loopexit(base, &ten_sec);`` event_base_dispatch(base); puts("Tick"); }

The main purpose of events is to inform some busy thread about some event that happened elsewhere. So, this looks logical.

I think the event flag EV_PERSIST mentioned in event.h File Reference might help.

Persistent event: won't get removed automatically when activated.

When a persistent event with a timeout becomes activated, its timeout is reset to 0.

Instead of

//...
event_stdout = event_new(eb, fd_stdout, EV_WRITE, &event_callback, NULL);
//...

you can pass this flag to event_new

//...
event_stdout = event_new(eb, fd_stdout, EV_WRITE|EV_PERSIST, &event_callback, NULL);
//...

and the other parts of code remain unchanged. At this time you create and add an event once and there's no need to call event_base_loop within a loop. The compiled program just keeps printing lines of "hello" until it is terminated.

By the way, I noticed that changing

write(fd, "hello\n", 7);

into

write(fd, "hello\n", 6);

eliminates the leading character '\\0' of each line.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM