3

I want to understand some question of high-performance server program which use Multithreading.

  1. epoll can handle TCP socket listening and socket connect,and i can use epoll_wait in the main thread, if there are any socket connect comming, program can accept connections and recv data in the work thread. there is no conflict between work thread. am i right?

    udp is an connectionless protocol,can we use recvfrom function in the all of work thread at the same time? the main thread just use epoll to notice the work thread receive data. (assume that i can process each UDP packet individually).

  2. this is a UDP server. i set it work with non-blocking socket. i receive data in the main thread and handle it , then send it to the task queue , if the task queue have data, the program wake up the work thread and lock the task queue ,then get the data from task queue, unlock the task queue so that other work thread can get task from task queue, and then handle it.

    is this a good multithread UDP server?i am not sure. is there another design of UDP server with high-performance。</p>

    I am in a puzzle about this questions, thank you very much!</p>

4

1 回答 1

3

Your basic approach is right. Start by studying the infamous C10K problem and how it was overcome. Once you understand the major bottlenecks in various implementations, you need to consider the following as part your design process :

  • Minimise thread creation/deletion cycles.
  • Always choose an "event-based" model over a blocking model.

Basically the one-thread-per-request model is preferred for its simplicity of implementation. But it does NOT scale well with the number of concurrent requests. When designing systems required to support more than a few 1000 concurrent requests, one prefers the use of sockets over threads.

The optimum number of "worker-threads" to instantiate depends upon :

  • Load (number of concurrent requests)
  • System (CPU, RAM)

Since this is a very popular problem when implementing web-server designs, you can find several analyses like this one, by simply searching for Apache vs. Nginx.

于 2013-08-17T02:27:12.763 回答