c++multithreadinglibevent

How to break out libevent's dispatch loop


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.


Solution

  • 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#_stopping_the_loop