[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



On Mon, Jun 24, 2013 at 3:52 AM, åéé <sunyucong@xxxxxxxxx> wrote:
> Hi,
>
> I am trying to convert a select() based program into using libevent, but i
> am having trouble to construct a way to do exactly like
>
> select() with a timeout of 1 second.
>
> basically I want to wait for as long as 1 second of possible events and move
> on to do other stuff in while(1) loop.
>
> I tried :
>
>         event_base_loopexit(g_event_base, &timeout);
>         event_base_loop(g_event_base, EVLOOP_ONCE);
>
>
> But I found that after several successful event triggers, the loop became
> shorter and shorter, Eg. after one event succesffuly triggered, the loop
> actually ran only 0.5 seconds next time, next time it is roughly 0.25
> seconds.The more events have triggered once, the faster the loop became.
>
> Any idea what to do? And If I was doing it wrong, what is the right way?

EVLOOP_ONCE means "exit right after processing the first batch of
events", so you probably don't want that one.  And the
event_base_loopexit() calls will start to stack up: each one creates a
new one-time event that makes the loop exit.

Instead, I'd suggest making a one-second timer event with a callback
that uses event_base_loopexit(g_event_base, NULL) to exit the event
loop.

yrs,
-- 
Nick
***********************************************************************
To unsubscribe, send an e-mail to majordomo@xxxxxxxxxxxxx with
unsubscribe libevent-users    in the body.