Search code examples
multithreadingasynchronousepolllibevent

libevent / epoll number of worker threads?


I am following this example. Line#37 says that number of worker threads should be equal of number of cpu cores. Why is that so?

If there are 10k connections and my system has 8 cores, does that mean 8 worker threads will be processing 10k connections? Why shouldn't I increase this number?


Solution

  • Your question is even better than you think! :-P

    If you do networking with libevent, it can do non-blocking I/O on sockets. One thread could do this (using one core), but that would under-utilize the CPU.

    But if you do “heavy” file I/O, then there is no non-blocking interface to the kernel. (Many systems have nothing to do that at all, others have some half-baked stuff going on in that field, but non-portable. –Libevent doesn’t use that.) – If file I/O is bottle-necking your program/test, then a higher number of threads will make sense! If a hard-disk is used, and the i/o-scheduler is reordering requests to avoid disk-head-moves, etc. it will depend on how much requests the scheduler takes into account to do its job the best. 100 pending requests might work better then 8.

    Why shouldn't you increase the thread number?

    If non-blocking I/O is done: all cores are working with thread-count = core-count. More threads only means more thread-switching with no gain.

    For blocking I/O: you should increase it!