[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[Libevent-users] Help with Libevent multi-threaded HTTP server



Hi,

I am new to lib event so please forgive me if this is a noob question. I ran into a problem usinglib event and wonder if anyone can help

My question I hope is very simple: -

I am trying to create a simple multi-threaded HTTP server.  

I create and bind a non-blocking listening socket on port 8080, then for each thread I want to create to process HTTP requests I call the following

event_base_new()
evhttp_new()
evhttp_accept_socket_with_handle()
evhttp_set_gencb()
pthread_create(my_http_worker_thread_fn, base);

Then in the thread function for each worker thread I call event_base_dispatch((struct event_base*)arg);

Everything works as expected and I can process HTTP requests. The problem I am having is shutting down cleanly. 

After my main thread creates the worker threads I wait for a return key press on getchar() so I can press return to shutdown, after which I call event_base_loopbreak() on each event base I created, then I call pthread_join() from my main thread on each worker thread handle to wait for all threads to complete. 

The problem is, after calling event_base_loopbreak() I am expecting event_base_dispatch() to return but it never does, looking at the call stack for each thread they all appear to be waiting for an event. So I end up stuck waiting for threads to end which never do.

I am on OS X

Can anyone tell me what I am doing wrong/should be doing? Any help would be very much appreciated. 

Thanks,