New thread per client connection in socket server?

2019-04-24 00:54发布

I am trying to optimize multiple connections per time to a TCP socket server.

Is it considered good practice, or even rational to initiate a new thread in the listening server every time I receive a connection request?

At what time should I begin to worry about a server based on this infrastructure? What is the maximum no of background threads I can work, until it doesn't make any sense anymore?

Platform is C#, framework is Mono, target OS is CentOS, RAM is 2.4G, server is on the clouds, and I'm expecting about 200 connection requests per second.

1条回答
淡お忘
2楼-- · 2019-04-24 01:42

No, you shouldn't have one thread per connection. Instead, you should be using the asynchronous methods (BeginAccept/EndAccept, BeginSend/EndSend, etc). These will make much more efficient use of system resources.

In particular, every thread you create adds overhead in terms of context switches, stack space, cache misses and so on. Linux is better at managing this stuff than Windows, for example, but that shouldn't be an excuse to give you free reign to create as many threads as you like ;)

查看更多
登录 后发表回答