具有libevent的多线程HTTP服务器 [英] Multi-threaded HTTP server with libevent

查看:547
本文介绍了具有libevent的多线程HTTP服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用libevent完成一个简单的HTTP服务器,并根据文档示例设法做到这一点。但是,没有线程,libevent的全部目的就是垃圾。我对C ++ 11中的线程不是很有经验,但是我很想知道如何正确实现这种服务器。

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.

我在网上找到了这个示例: https://gist.github.com/kzk/665437

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

这正确吗? pthread是正确的选择吗?而且,这一行很奇怪:

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);
}

那里发生了什么?

推荐答案

我还不推荐libevhtp,因为严重的错误,但是您可能想看看它们如何使用线程: https://github.com/ellzey/libevhtp/blob/master/examples/thread_design.c

-他们正在创建单独的libevent实例,每个线程一个。然后,只要您小心地在线程中使用相同的libevent基,所有异步代码都将在没有额外锁的情况下工作。 IMO是针对典型Web服务器进行libevent培训的最佳方法。

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.

对于 https://gist.github.com/kzk/665437 ,c ++ 11线程应该不会比pthreads差。

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

这篇关于具有libevent的多线程HTTP服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆