Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Server Thread Allocation

I am writing a server that needs to serve a large number of clients. I am considering what is the best thread strategy to use: I read that the ThreadPool class on the .NET Framework allocates threads after taking into account parameters like the number of cores the machine is running on, which is great. However, if there is no thread available, it waits for one to become available.

The connections on my sockets may be fairly long, i.e. a thread may run for quite some time before it is done serving its client and terminating. Therefore, if I start a new thread for every socket, it is possible in theory for a large number of threads to be idle (waiting for data on the socket), yet still considered to be running, and thus preventing the ThreadPool from allocating new threads, and serving other clients. On the other hand, using a predefined number of threads to serve all sockets does not make an optimal use of the machine's multiple cores.

I am assuming there is a better way to do this... Any suggestions?

Thank you.

like image 568
kman Avatar asked Sep 02 '25 15:09

kman


1 Answers

You want to be using asynchronous sockets. They utilize the thread pool but do not use up threads while waiting for data on the socket (i.e. they are non-blocking).

like image 79
Kirk Woll Avatar answered Sep 04 '25 06:09

Kirk Woll