Hi,

I've run into an issue that surprised me a bit. I'm installing a few
timeouts to be
executed on a following loop iteration, i.e. after zero seconds. I
would expect the timeouts
happen in the order they were installed, however, this is not the
case. Here's the code
that shows the issue:

#include <stdio.h>
#include "event.h"

struct event e[3];
struct timeval zero = {0, 0};

void print(int fd, short events, char * text)
{
        printf(text);
}

void callback(int fd, short events, void * arg)
{
        timeout_set(&e[0], print, "A\n");
        timeout_add(&e[0], &zero);
        timeout_set(&e[1], print, "B\n");
        timeout_add(&e[1], &zero);
        timeout_set(&e[2], print, "C\n");
        timeout_add(&e[2], &zero);
}

int main()
{
        struct event e;
        event_init();
        timeout_set(&e, callback, NULL);
        timeout_add(&e, &zero);
        event_dispatch();
}

The output of this program is
libevent$ ./a.out
A
C
B

not ABC as I would like.

If I do the same outside of the callback, the order is maintained as expected.

Adding more timers suggests that all of them except the first one (A)
are executed in the reverse order.
Again, doing the same outside of a callback makes them run in the
expected order.

Is this a bug or it's supposed to work this way? Maybe I'm doing
something wrong and there is a way
of getting the expected behavior without resorting to increasing
timeval for each new call?

Thanks,
Denis.
_______________________________________________
Libevent-users mailing list
Libevent-users@monkey.org
http://monkeymail.org/mailman/listinfo/libevent-users

Reply via email to