Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

New thread per client connection in socket server?

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.

like image 558
Olaseni Avatar asked Mar 24 '10 05:03

Olaseni


1 Answers

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 ;)

like image 117
Dean Harding Avatar answered Nov 09 '22 23:11

Dean Harding