Reputation: 113
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.
Upvotes: 4
Views: 548
Reputation: 35905
I highly recommend this video, Jeffrey Richter gives a talk about general multithreading and thread pooling. He also mentions the best strategies for server multithreading
Upvotes: 0
Reputation: 4381
The .NET ThreadPool
is much smarter than that. I'd recommend using either the Task/TaskScheduler
framework or the APM model of the related FCL classes. That'll almost certainly be superior to any manual thread spawning strategy - unless you come up with one which beats'em all...
Upvotes: 0
Reputation: 77546
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).
Upvotes: 4
Reputation: 51329
Don't allocate threads yourself. Use IIS, or Windows Process Activation Service: http://msdn.microsoft.com/en-us/library/ms733109.aspx
Upvotes: 0