开发者

Why have multiple threads on a server

I am creating an server to send data to many persistent sockets. I have chosen the REACTOR design pattern which suggests having multiple threads to send data to along sockets.

I cannot understand what is better: - To have one thread to send all of the data to sockets - Or have a couple of threads to send data across the sockets.

The way I see it is that I have 2 cores. So I can on开发者_如何学Goly do two things at once. Whcih would mean I have 1 worker thread and 1 thread to send data?

Why would it be better to have mulitple threads to send data when you suffer context switching between the threads?


See documentation on thttpd as to why single threaded non blocking IO is good. Indeed it makes good sense for static files.

If you are doing CGI however, you may have a script that runs for a long time. It's nicer to not hold up all the quicker simpler traffic, especially if the script has an infinite-loop bug in it and is to eventually be killed anyway! With threads the average response time experienced by users will be better - if some of the requests are very time consuming.

If the files being served come from disk and are not in main memory already, a similar argument can be used.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜