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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With