Not to terminate ev_loop

Sandeep sandeep at b-one.net
Sun Jan 8 08:51:51 CET 2012


No, I did not copy that. Ok, I will simply my problem statement to as
minimal as I can.

 

If I take the example code as below:

 

As of now the below program will print "stdin-ready" only once as soon as
user enters input and then exits

My intention is to make it into a program where it will all the time be
running waiting for user input to STDIN (not just 1 shot), and each time
user enters an input the program must print "stdin-ready" without exiting.

 

In my experimentation with very minimal knowledge and time, I have
intentionally commented out the functions ev_io_stop and ev_unloop in both
stdin_cb and timeout_cb, but as expected what is happening is,

"stdin-ready" gets called as soon as user enters something and immediately
stdin-ready is called in an infinite loop.

Could you please help me how to modify this in such a way that, call
"stdin_cb" everytime there is input (but not simply in infinite loop), if
there is no input just keep waiting without exiting.

 

#include <ev.h>

#include <stdio.h>

 

ev_io stdin_watcher;

ev_timer timeout_watcher;

 

static void

  stdin_cb (EV_P_ struct ev_io *w, int revents)

  {

    puts ("stdin ready");

 

    // for one-shot events, one must manually stop the watcher

    // with its corresponding stop function.

 

    //ev_io_stop (EV_A_ w);

 

        // this causes all nested ev_loop's to stop iterating

    //ev_unloop (EV_A_ EVUNLOOP_ALL);

  }

 

static void

  timeout_cb (EV_P_ struct ev_timer *w, int revents)

  {

    puts ("timeout");

 

    // this causes the innermost ev_loop to stop iterating

    //ev_unloop (EV_A_ EVUNLOOP_ONE);

  }

 

 

int

  main (void)

  {

    struct ev_loop *loop = ev_default_loop (0);

    ev_io_init (&stdin_watcher, stdin_cb, /*STDIN_FILENO*/ 0, EV_READ);

    ev_io_start (loop, &stdin_watcher);

 

 

    ev_timer_init (&timeout_watcher, timeout_cb, 5.5, 0.);

    ev_timer_start (loop, &timeout_watcher);

    

    ev_loop (loop, 0);

 

    // unloop was called, so exit

    return 0;

  }

 

 

From: John Newton [mailto:jnewton at gmail.com] 
Sent: Sunday, January 08, 2012 11:30 AM
To: Sandeep
Subject: Re: Not to terminate ev_loop

 

Did you copy the example code that calls the following?

 

 
ev_break (EV_A_ EVBREAK_ONE);

ev_break() tells the ev_loop() to exit.

 

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.schmorp.de/pipermail/libev/attachments/20120108/70969846/attachment.html>


More information about the libev mailing list