Multi-threaded HTTP server with libevent

穿精又带淫゛_ 提交于 2019-12-13 04:49:49

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/20426251/multi-threaded-http-server-with-libevent

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