Question

I'm trying to get a simple HTTP server done with libevent and managed to do it based on the documentation examples. However, without threads, the whole purpose of libevent is garbage. I'm not very experienced with threads in C++11, but i would love to know how to properly implement such server.

I found this example online: https://gist.github.com/kzk/665437

Is this correct? Is pthreads the proper choice? Also, this line is very strange:

for (int i = 0; i < nthreads; i++) {
    pthread_join(ths[i], NULL);
}

What's going on there?

Was it helpful?

Solution

I can't recommend libevhtp yet, because of a serious bug, but you might want to look at how they use the threads: https://github.com/ellzey/libevhtp/blob/master/examples/thread_design.c
- They are creating separate libevent instances, one for each thread. All the asynchronous code will then just work without extra locks etc as long as you are careful to use the same libevent base in a thread. IMO it's the best approach to libevent theading for a typical web server.

As for https://gist.github.com/kzk/665437, c++11 threading shouldn't be any worse than pthreads.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top