Search code examples
eventslibeventevent-loopansi-c

Libevent, add or remove events dynamically without having to change the event loop


I'm facing a problem that may be a misunderstanding of what this sentence really means "An application just needs to call event_dispatch() and then add or remove events dynamically without having to change the event loop." or I can't find the right documentation of how to do it. Well, the problem is that I think that I should be able to add events to the event loop after running it with event_dispatch() but I can't get it working. Here is the code:

#include <event2/event.h>
#include <event2/buffer.h>
#include <event2/bufferevent.h>

#include <stdio.h>

static int n_calls = 0;
static int n_calls2 = 0;

void cb_func(evutil_socket_t fd, short what, void *arg)
{
    struct event *me = arg;

    printf("cb_func called %d times so far.\n", ++n_calls);

    if (n_calls > 100)
       event_del(me);
}

void cb_func2(evutil_socket_t fd, short what, void *arg)
{
    struct event *me = arg;

    printf("cb_func2 called %d times so far.\n", ++n_calls2);

    if (n_calls2 > 100)
       event_del(me);
}

int main(int argc, char const *argv[])
{
    struct event_base *base;
    enum event_method_feature f;

    base = event_base_new();
    if (!base) {
        puts("Couldn't get an event_base!");
    } else {
        printf("Using Libevent with backend method %s.",
            event_base_get_method(base));
        f = event_base_get_features(base);
        if ((f & EV_FEATURE_ET))
            printf("  Edge-triggered events are supported.");
        if ((f & EV_FEATURE_O1))
            printf("  O(1) event notification is supported.");
        if ((f & EV_FEATURE_FDS))
            printf("  All FD types are supported.");
        puts("");
    }

    struct timeval one_sec = { 1, 0 };
    struct timeval two_sec = { 2, 0 };
    struct event *ev;
    /* We're going to set up a repeating timer to get called called 100 times. */
    ev = event_new(base, -1, EV_PERSIST, cb_func, NULL);
    event_add(ev, &one_sec);

    event_base_dispatch(base);

    // This event (two_sec) is never fired if I add it after calling event_base_dispatch. 
    // If I add it before calling event_base_dispatch it works as the other event (one_sec) also does.
    ev = event_new(base, -1, EV_PERSIST, cb_func2, NULL);
    event_add(ev, &two_sec);

    return 0;
}

Solution

  • I see it now... I don't know why but I was thinking that the event-loop started running in another thread or something like that. I see now that what I was trying to do has no sense. You can add events inside the callbacks, that is, when the loop is running. When you start the event-loop, it never returns so everything after that will never be called (unless you stop the event-loop)