tags:

views:

82

answers:

2

I know .NET has a good asynchronous model for network I/O, which uses completion port under the hood. But all the callbacks are happening on IO threads from thread pool.

Is there any .NET counterparts like Java's selector which deals with multiple streams in a single thread? Or, the thread pool callbacks scale better than this single threaded approach?

Thanks,

Dodd

+1  A: 

For async operations an IO handle is associated with the thread pool. When the async operation completes, I believe that the callbacks(for each stream) may or may not execute using the same thread, any available thread pool thread could process the callback, it's quite possible that the same thread could process multiple callbacks or just one callback based on runtime conditions.

Hope this helps

EDIT: Adding reply to Dodd's comment

I'm not intimately familiar with the Selector API but from looking at an example posted here it seems that Selector waits until all events occur. Is that true? If so then the caller would have to wait for all events to occur even when one event occurs sooner than the another. But if the Selector works by processing an event as soon as it occurs, one could run into a situation where the selector is processing the callback for one event while another event arrives(I would imagine that in this case the incoming event would get queued somewhere or else you would be dropping events) but it would still reduce the throughput when the events are orthogonal and should be processed as soon as they occur.

The async model in .NET is centered around the thread pool to reduce the overhead of creating a new thread(since it's an expensive operation). If you are observing that the thread pool is maxing out you could increase the number of Thread in the pool as documented here. Bear in mind though that at the end of the day you are limited to the number of processors i.e On a dual core box only 2 threads can be actively running, all others are blocked, so that might be something to take into account.

Hope this helps.

Abhijeet Patel
The ThreadPool and the scheduling algorithm is undergoing a major overhaul in .NET4 to support TPL and perform smarter scheduling of threads(Tasks)
Abhijeet Patel
A: 

Thanks Abhijeet,

Well, my concern is that, in some extremely busy senarios, many async callbacks happen simultaneously and we are running out of threads: then context switching will be nightmare. In this particular case, is async callback the right choice? Or we should use Select()?

Microsoft have had eight years now to discover whether this is a problem.
John Saunders
Hey Dodd, this should really be a comment on @Abhijeet's question, not a new answer (welcome to SO by the way.)
Drew Noakes
@Dodd: I'll add my reply (below)
Abhijeet Patel