Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are Socket.*Async methods threaded?

I'm currently trying to figure what is the best way to minimize the amount of threads I use in a TCP master server, in order to maximize performance.

As I've been reading a lot recently with the new async features of C# 5.0, asynchronous does not necessarily mean multithreaded. It could mean separated in smaller chunks of finite state objects, then processed alongside other operations, by alternating. However, I don't see how this could be done in networking, since I'm basically "waiting" for input (from the client).

Therefore, I wouldn't use ReceiveAsync() for all my sockets, it would just be creating and ending threads continuously (assuming it does create threads).

Consequently, my question is more or less: what architecture can a master server take without having one "thread" per connection?

Side question for bonus coolness points: Why is having multiple threads bad, considering that having an amount of threads that is over your amount of processing cores simply makes the machine "fake" multithreading, just like any other asynchronous method would?

like image 799
Lazlo Avatar asked Mar 19 '11 00:03

Lazlo


1 Answers

No, you would not necessarily be creating threads. There are two possible ways you can do async without setting up and tearing down threads all the time:

  1. You can have a "small" number of long-lived threads, and have them sleep when there's no work to do (this means that the OS will never schedule them for execution, so the resource drain is minimal). Then, when work arrives (i.e. Async method called), wake one of them up and tell it what needs to be done. Pleased to meet you, managed thread pool.
  2. In Windows, the most efficient mechanism for async is I/O completion ports which synchronizes access to I/O operations and allows a small number of threads to manage massive workloads.

Regarding multiple threads:

Having multiple threads is not bad for performance, if

  • the number of threads is not excessive
  • the threads do not oversaturate the CPU

If the number of threads is excessive then obviously we are taxing the OS with having to keep track of and schedule all these threads, which uses up global resources and slows it down.

If the threads are CPU-bound, then the OS will need to perform much more frequent context switches in order to maintain fairness, and context switches kill performance. In fact, with user-mode threads (which all highly scalable systems use -- think RDBMS) we make our lives harder just so we can avoid context switches.

Update:

I just found this question, which lends support to the position that you can't say how many threads are too much beforehand -- there are just too many unknown variables.

like image 91
Jon Avatar answered Sep 28 '22 05:09

Jon