views:

44

answers:

1

I'm preparing to write a multithread network application. At the moment I'm wondering what's the best thread pattern for my program. Whole application will handle up to 1000 descriptors (local files, network connections on various protocols and additional descriptors for timers and signals handling). Application will be optimized for Linux. Program will run on regular personal computers, so I assume, that they will have at least Pentium 4.

Here's my current idea:

  • One thread will handle network I/O using epoll.
  • Second thread will handle local-like I/O (disk I/O, timers, signal handling) using epoll
  • Third thread will handle UI (CLI, GTK+ or Qt)

Handling each network connection in separate thread will kill CPU because of too many context switches.

Maybe there's better way to do this?

Do you know any documents/books about designing multirhread applications? I'm looking for answers on questions like: What's the rational number of threads? etc.

+1  A: 

You're on the right track. You want to use a thread pool pattern to handle the networking rather than one thread per network connection.

This website may also be helpful to you and lists the most common design patterns and in what situations they can be used. http://sourcemaking.com/design_patterns/

To handle the disk I/O you might like to consider using mmap under linux. It's very fast and efficient. That way, you will let the kernel do the work and you probably won't need a separate thread for that.

I'm currently playing with Boost::asio which seems to be quite good. It uses epoll on linux. As it appears you are using a cross platform gui toolkit like Qt, then boost asio will also provide cross platform support so you will be able to use it on windows or linux. I think there might be a cross platform mmap too.

Matt H