[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [Libevent-users] Multiple timers handling



On Sat, Jan 30, 2010 at 1:40 AM, Venkatesh Solaiyappan
<sovenkat@xxxxxxxxx> wrote:
> Hi Alejandro,
>
> I am looking to handle multiple timeout events in single callback.. I don't
> think it is not avaible today in libevent.
>
> I believe i can tag each timer fd (when creating the timer event) to a
> unique ENUM
> so that when my cb is called, i can findout the corresponding timeout ENUM
> and then handle appropriately...

Usually if you want to distinguish between multiple events with the
same callback, you do so using the user-data pointer, like this:

struct timeout_data {
   /* You'd want to put your own data here, not just a string. */
   const char *str;
};

void cb(evutil_socket_t ignore, short events, void *ptr)
{
    struct timeout_data *data = ptr;

    printf("Timeout with string '%s' elapsed\n", data->str);
}

void register_events(struct event_base *base) {
    /* I'm assuming libevent2 here, but the principle is the same */
    struct timeout_data *d1, *d2;
    struct event *timer1, *timer2;
    struct timeval five_sec = { 5, 0 }, thirty_sec = { 30, 0 };
    d1 = malloc(sizeof(struct timeout_data));
    d2 = malloc(sizeof(struct timeout_data));

    d1->str = "five-second timeout";
    event1 = evtimer_new(base, cb, d1);
    event_add(event1, &five_sec);

    d2->str = "thirty-second timeout";
    event2 = evtimer_new(base, cb, d2);
    event_add(event2, &thirty_sec);
}

You don't want to use fake fds; that will break with nearly all backends.
***********************************************************************
To unsubscribe, send an e-mail to majordomo@xxxxxxxxxxxxx with
unsubscribe libevent-users    in the body.