How to break out libevent's dispatch loop

淺唱寂寞╮ 提交于 2020-01-15 09:08:13

问题


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.

It boils down to the following C++ code:

#include <stdlib.h>
#include <signal.h>
#include <thread>
#include <evhttp.h>

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, "<html><body>Testing 1-2-3</body></html>");
    evhttp_send_reply(req, HTTP_OK, "OK", OutBuf);
}

void dispatch() {
    eb = event_base_new();
    struct evhttp *http = evhttp_new(eb);
    evhttp_set_gencb(http, &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(&sigIntHandler.sa_mask);
    sigIntHandler.sa_flags = 0;
    sigaction(SIGINT, &sigIntHandler, NULL);

    t = std::thread { &dispatch };

    while ( ! bailout ) {
        std::this_thread::sleep_for(std::chrono::seconds(1));
    }

    event_base_loopexit(eb, NULL);

    t.join();
}

The behavior is that if you run the program, request a page, abort the program by Ctrl-C, the event_base_dispatch() keeps running until you fetch another web page. Only then the loop aborts and the program terminates.


回答1:


If you need the event loop to exit before running any more callbacks, use event_base_break()

http://www.wangafu.net/~nickm/libevent-book/Ref3_eventloop.html




回答2:


In the end I implemented breaking out the dispatch loop by adding a timer which basically polls the bailout variable:

struct event *ev;
const struct timeval one_sec = { 1, 0 }; // sec, usec

void cb_timer_func(evutil_socket_t, short, void) {

  if ( bailout ) {
    event_base_loopbreak(eb);
  }
  else if ( ! evtimer_pending(ev, NULL) ) {
    evtimer_del(ev);
    evtimer_add(ev, &one_sec);
  }
}

And in the dispatch loop, I added:

ev = evtimer_new(eb, cb_timer_func);
evtimer_add(ev, &one_sec);



回答3:


One more approach is to put event_base_loopbreak() into a signal handler.



来源:https://stackoverflow.com/questions/29219314/how-to-break-out-libevents-dispatch-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!