0

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?

vinnylinux
  • 7,050
  • 13
  • 61
  • 127
  • You should read up on strategies how to handle multiple clients. One thread per client may be sub-optimal. http://www.kegel.com/c10k.html#strategies – Maxim Egorushkin Dec 06 '13 at 14:30
  • Also if you want to use threads and you are using C++11, you can take a look here: http://en.cppreference.com/w/cpp/thread/thread. – Ivan Genchev Dec 06 '13 at 14:32
  • _"What's going on there? "_ -- Those 3 lines join the threads without caring for their exit code. In other words, the server main process waits until they have all finished. – Damon Dec 06 '13 at 14:46
  • So, the above code is not really scalable, is it? – vinnylinux Dec 06 '13 at 14:57
  • Actually the code in https://gist.github.com/kzk/665437 is rather good scalability-wise: it uses a limited number of threads and libevent instances (one per thread) in order to better utilize the CPUs, while every libevent instance asynchronously maintains a multitude of connections from HTTP clients. Similar to what libevhtp does with evthr. – ArtemGr Dec 06 '13 at 15:21
  • I'm always curious to what seems to be the best approach: multiple processes being spawned or multiple threads. I'm studying the best approach to handle many concurrent connections at once, i thought libevhtp would be the best choice. Or that snippet does it better? – vinnylinux Dec 06 '13 at 15:26
  • As I've already said they use a nearly identical approach. – ArtemGr Dec 06 '13 at 15:40

1 Answers1

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.

ArtemGr
  • 11,684
  • 3
  • 52
  • 85
  • They are different standards in different languages. – ArtemGr Dec 06 '13 at 15:10
  • I see. Even though it has this serious bug, seems like a good base to start working on! – vinnylinux Dec 06 '13 at 15:15
  • The libevent http server doesn't know how to work with HTTP pipelining too, BTW, so you shouldn't use either libevent or libevhtp as a front-end server. Though the libevent http server *might* fail in a more graceful way when the client tries to pipeline HTTP requests - I haven't tried it myself. – ArtemGr Dec 06 '13 at 15:25