简体   繁体   English

具有libevent的多线程HTTP服务器

[英]Multi-threaded HTTP server with libevent

I'm trying to get a simple HTTP server done with libevent and managed to do it based on the documentation examples. 我正在尝试用libevent完成一个简单的HTTP服务器,并根据文档示例设法做到这一点。 However, without threads, the whole purpose of libevent is garbage. 但是,没有线程,libevent的全部目的就是垃圾。 I'm not very experienced with threads in C++11, but i would love to know how to properly implement such server. 我对C ++ 11中的线程不是很有经验,但是我很想知道如何正确实现这种服务器。

I found this example online: https://gist.github.com/kzk/665437 我在网上找到了这个例子: https : //gist.github.com/kzk/665437

Is this correct? 这个对吗? Is pthreads the proper choice? pthread是正确的选择吗? Also, this line is very strange: 另外,这一行很奇怪:

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

What's going on there? 那里发生了什么事?

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 由于存在严重的错误 ,我不推荐libevhtp,但是您可能想看看它们如何使用线程: https : //github.com/ellzey/libevhtp/blob/master/examples/thread_design.c
- They are creating separate libevent instances, one for each thread. -他们正在创建单独的libevent实例,每个线程一个。 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. 然后,只要您小心地在线程中使用相同的libevent基,所有异步代码都将在没有额外锁的情况下工作。 IMO it's the best approach to libevent theading for a typical web server. IMO是用于典型Web服务器的libevent theading的最佳方法。

As for https://gist.github.com/kzk/665437 , c++11 threading shouldn't be any worse than pthreads. 至于https://gist.github.com/kzk/665437,c ++ 11线程应该不会比pthreads差。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM