title: libevent - event_base_loop() should it get events repeatly?
body: <p>Here is a simple program using <code>libevent</code> on linux, it tracks the <code>stdout</code> fd, when it's writable, the callback will print some info to <code>stdout</code>.</p>
  
  <hr>
  
  <h2>Code</h2>
  
  <p><strong>hello_libevent.c:</strong></p>
  
  <pre><code>// libevent hello
  #include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;unistd.h&gt;
  
  #include &lt;event2/event.h&gt;
  #include &lt;event2/thread.h&gt;
  
  
  void event_callback(evutil_socket_t fd, short events, void *arg) {
      if(events | EV_WRITE) {
          write(fd, "hello\n", 7);
      }
  
      sleep(1);
  }
  
  int libevent_test() {
      int opr;
  
      // enable pthread
      if(evthread_use_pthreads() == -1) {
          printf("error while evthread_use_pthreads(): %s\n", strerror(errno));
          return -1;
      }
  
      // create event_base
      struct event_base* eb;
      if((eb = event_base_new()) == NULL) {
          printf("error while event_base_new(): %s\n", strerror(errno));
          return -1;
      }
  
      // create event
      int fd_stdout = fileno(stdout);
      struct event* event_stdout;
      event_stdout = event_new(eb, fd_stdout, EV_WRITE, &amp;event_callback, NULL);
  
      // add event as pending
      struct timeval timeout = {10, 0};
      if(event_add(event_stdout, &amp;timeout) == -1) {
          printf("error while event_add(): %s\n", strerror(errno));
          return -1;
      }
  
      // dispatch
      if((opr = event_base_loop(eb, EVLOOP_NONBLOCK)) == -1) {
          printf("error while event_base_dispatch(): %s\n", strerror(errno));
          return -1;
      } else if(opr == 1) {
          printf("no more events\n");
      } else {
          printf("exit normally\n");
      }
  
      // free event
      event_free(event_stdout);
  
      return 0;
  }
  
  int main(int argc, char * argv[]) {
      return libevent_test();
  }
  </code></pre>
  
  <p><strong>Compile:</strong></p>
  
  <blockquote>
    <p>gcc -Wall hello_libevent.c -levent -levent_pthreads</p>
  </blockquote>
  
  <p><strong>Execution result:</strong></p>
  
  <pre><code>hello
  no more events
  </code></pre>
  
  <hr>
  
  <h2>Questions:</h2>
  
  <ul>
  <li>In the test, event only occur once, is that the expected behavior? Or it should loop to get more event until timeout?</li>
  <li>How to make it get event continuously? Is it necessary to call <code>event_base_loop</code> within a loop, while it's already a <code>loop</code>?</li>
  </ul>