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

Re: [Libevent-users] How to make a eventloop similiar to select() with a timeout of 1sec



Thanks everyone, here is the final solution that I am in use now,
works pretty good.

void exit_after_one_second(evutil_socket_t fd, short events, void *arg)
{
  event_base_loopbreak((struct event_base *)arg);
}

int run_for_at_most_one_second(struct event_base *base)
{
  int r;
  static struct event *ev = NULL;
  struct timeval one_second = {1, 0};

  if (ev == NULL)
    ev = evtimer_new(base, exit_after_one_second, base);

  event_add(ev, &one_second);
  r = event_base_loop(base, EVLOOP_ONCE);
  return r;
}


On Mon, Jun 24, 2013 at 3:16 PM, Vincent Bernat <bernat@xxxxxxxx> wrote:
>
>  â 24 juin 2013 20:34 CEST, åéé <sunyucong@xxxxxxxxx> :
>
> > The server operates in a mode that basically can be described as following:
> >
> > while(1) {
> > do_some_global_stuff();
> >
> > gather_possible_user_input_and_send_output():
> >
> > executing_only_one_command_for_each_client_in_sequence();
> >
> > do_some_other_global_stuff();
> > }
>
> Not tested (maybe we cannot pass a NULL callback):
>
>     struct event *to = NULL;
>     do {
>        if (event_base_got_exit(base) || event_base_got_break(base)) break;
>        if (to) event_free(to);
>
>        /* Do your global stuff here. */
>
>        struct timeval one_second = {1,0};
>        to = evtimer_new(base, NULL, base);
>        event_add(to, &one_second);
>     } while (event_base_loop(base, EVLOOP_ONCE) == 0);
>     event_free(to);
>
> Either there is nothing to do and an event will be triggered by the one
> second timeout or there is something to do and you cancel the timeout.
> --
> panic("Lucy in the sky....");
>         2.2.16 /usr/src/linux/arch/sparc64/kernel/starfire.c
***********************************************************************
To unsubscribe, send an e-mail to majordomo@xxxxxxxxxxxxx with
unsubscribe libevent-users    in the body.