Regd ev_timer

Jann Horn jann+libev at
Tue Apr 2 08:53:53 CEST 2013

On Tue, Apr 02, 2013 at 02:48:05PM +1100, Varun Chandramohan wrote:
> Hi Folks,
> Iam trying to understand the timer behaviour using a simple program. Let me
> first post the pseudo code.

Why pseudo code? Why not your real code? If you have made a mistake that's not visible
in your pseudo code anymore, we can't spot it. And we can't run your pseudo code.
By the way, every second line of the code is blank – that's hard to read on a small
display (I reformatted it so that it's easier to read for me). Also, your code isn't
properly indented.
I can't find an error in your pseudocode, but I'll comment on it anyway.

> static void hb_conn_cb(struct ev_loop *loop, struct ev_io *w, int events) {
>     struct ps_hb *ps_hb = (struct ps_hb *)w;
>     printf("HB Timer\n");
>     if (EV_WRITE & events) {
>         ev_io_stop(loop, &ps_hb->io);
>         ev_timer_stop(loop, &ps_hb->timer);
>         close(ps_hb->sd);
>         sleep(10);
>         if ((ps_hb->sd = socket(AF_INET, SOCK_STREAM, 0)) == -1) return;
>         if (set_socket_nonblock(ps_hb->sd)) return;
>         ev_io_set(&ps_hb->io, ps_hb->sd, EV_WRITE);
>         ev_timer_set(&ps_hb->timer, DEFAULT_CONNECT_TIMER + ev_now (loop) -
> ev_time (), 0);

This trick with the timer is a bit ugly. Why don't you do it the way it's
documented in the manual? Just do

    ev_timer_set(&ps_hb->timer, DEFAULT_CONNECT_TIMER, 0);

(same thing in hb_timeout_handler)

>         ev_io_start(loop, &ps_hb->io);
>         ev_timer_start(loop, &ps_hb->timer);
>        connect();

Is this connect() part of that block? It's indented weirdly and the curly brace
is missing, so I can't tell. (Another reason why you should have posted real code.)
(again, same thing in hb_timeout_handler)

>     return;
> }

> As you can see its a pretty simple program and my intention is to do a
> connect to say "" periodically every 10 sec (originally 2 mins)
> and see if its connects or not.

If your intention is to check whether the internet is reachable, you should try
connecting to an IP, not to a hostname. Connecting to a hostname usually means
that you have to do a DNS lookup which might also block. Just use or so
(that's a public DNS server by Google).

> Since by default connect timeout is too
> long for me I want set sd to non-block and set a timeout for
> DEFAULT_CONNECT_TIMER. That is if that timer exipres and we have not
> connected then I will not connect to failed. If the timer does
> not expire and connect happens before that then I simply reset the timer
> and do this forever.

Sounds good.

> However, the above code does not work because of using sleep(10) which is
> 11.0 then everything works as planned. But I do not want connect to have
> such big timeout. Is there a way I can make the timer stop and not record
> events when Iam sleeping?

Huh? What do you mean, "record events"? A timer does not record events, all it
does is fire after the specified delay is over.

> Iam not quite sure why this must happen even
> after I did  ev_timer_stop(loop, &ps_hb->timer).

After you have called ev_timer_stop, the timer will not fire anymore.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <>

More information about the libev mailing list