First of all, it is important to know whether what you are doing is reasonable. If they are all connected to the CPU, then not , IMO; The web server is already very multithreaded, and, as a rule, this is a busy place to start. The chances are high that you will slow it all down using multiple cores. It will look great for 1 user!
If you are attached to IO, then there are several ways to do this; it would be preferable to use the built-in asynchronous methods that you are talking to, so you can use IOCP rather than regular threads. So for NetworkStream use BeginRead(...) etc.
Then you need to combine everything together. Many ways; I personally tend to use Monitor.Wait and Monitor.Pulse , as this avoids unmanaged code (many wait descriptors are actually provided by the OS).
Also note: threading / parallelism is included with many interesting ways to fail; as a rule, you just need to worry too much about static methods / data for synchronization, but if you have multiple threads in one request doing something: be careful ... there are a lot.
The next version of .NET is designed to make the continuation much easier; I need to see how easily we can apply the current experimental code to IOCP scripts.
Marc gravell
source share