c++libevent

Need suggestion on dynamically changing timer event with libevent


I am currently using libevent-2.0.5 in my cpp (C++11) program to set up a timer event.

The code logic is like:

    #include <stdio.h>
    #include <sys/time.h>
    #include <event.h>

    static void timer_cb(evutil_socket_t fd, short what, void *arg)
    {
        printf("something\n");
    }
  
    int main(int argc, char* argv[])
    {
        struct event *foo_timer;
        struct timeval one_sec = {.tv_sec = 1};
        struct event_base *base = event_base_new();

        // set up libvent timer to be executed every 1 second
        foo_timer = event_new(base, -1, EV_TIMEOUT|EV_PERSIST, timer_cb, args);
        event_add(foo_timer, &one_sec);

        // dispatch event
        event_base_dispatch(base);

    }

So the callback function will be executed every 1 sec.

Now my question is, how can I change the interval dynamically?

For example, assume there is another thread sending signal (don't know when) to this timer callback such as setting a bool flag. What I want to achieve is that when the callback finds that flag is true, it can change the time interval to 500ms. In such a way, the timer callback will be executed 500ms later after the current one finishes.

I am now stuck here because the only possible way I find out is to remove that timer and create (add) a new one. like:

    static void timer_cb(evutil_socket_t fd, short what, void *arg)
    {
        // do some checking 
        ...
        // create new event and remove the old one
        if (flag)
        {
            // create new timer
            bar_timer = event_new(base, -1, EV_TIMEOUT|EV_PERSIST, timer_cb, bar_timer);
            event_add(bar_timer, &five_100ms);
       
            // remove old one
            old_event = (struct event *)args;
            event_del(old_event);
        }
    }
  
    int main(int argc, char* argv[])
    {
        struct event *foo_timer;
         ...
        // set up libvent timer to be executed every 1s
        foo_timer = event_new(base, -1, EV_TIMEOUT|EV_PERSIST, timer_cb, foo_timer);
        event_add(foo_timer, &one_sec);
        // dispatch
         ...
    }

But I get crashes after event_del(old_event); If I give up event_del, the new event seems to be not triggered.

So I need some suggestion on how to achieve my purpose, or should I turn to some other libs to achieve that?


Solution

  • yes the only way is to delete the old event and create a new event with 500ms timeout. Check if pointer(old_event) is valid.

            old_event = (struct event *)args;
            event_del(old_event);
    

    My suggestion would be to use event_self_cbarg available from version Libevent 2.1.1-alpha.