title: How to break out libevent's dispatch loop
body: <p>I've implemented a tiny webserver in a separate thread using the nice libevent library. The webserver runs event_base_dispatch() to process all events. What I need is a way to break out this dispatch loop from the main thread. </p>
  
  <p>It boils down to the following C++ code:</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;signal.h&gt;
  #include &lt;thread&gt;
  #include &lt;evhttp.h&gt;
  
  struct event_base *eb;
  std::thread t;
  
  static volatile sig_atomic_t bailout = false;
  
  void my_signal_handler(int) {
      bailout = true;
  }
  
  void onRequest(evhttp_request *req, void *) {
      struct evbuffer *OutBuf = evhttp_request_get_output_buffer(req);
      evbuffer_add_printf(OutBuf, "&lt;html&gt;&lt;body&gt;Testing 1-2-3&lt;/body&gt;&lt;/html&gt;");
      evhttp_send_reply(req, HTTP_OK, "OK", OutBuf);
  }
  
  void dispatch() {
      eb = event_base_new();
      struct evhttp *http = evhttp_new(eb);
      evhttp_set_gencb(http, &amp;onRequest, NULL);
      evhttp_bind_socket_with_handle(http, "0.0.0.0", 5555);
      event_base_dispatch(eb);
  }
  
  int main() {
  
      struct sigaction sigIntHandler;
      sigIntHandler.sa_handler = my_signal_handler;
      sigemptyset(&amp;sigIntHandler.sa_mask);
      sigIntHandler.sa_flags = 0;
      sigaction(SIGINT, &amp;sigIntHandler, NULL);
  
      t = std::thread { &amp;dispatch };
  
      while ( ! bailout ) {
          std::this_thread::sleep_for(std::chrono::seconds(1));
      }
  
      event_base_loopexit(eb, NULL);
  
      t.join();
  }
  </code></pre>
  
  <p>The behavior is that if you run the program, request a page, abort the program by Ctrl-C, the event_base_dispatch() keeps running <em>until</em> you fetch another web page. Only then the loop aborts and the program terminates.</p>