views:

274

answers:

1

Using a delegate I can call any function asynchronously. From the documentation I understand this is done by queueing a workitem for the threadpool.

One can also do asynchronous calls to IO functions (like reading from sockets, files, webpages, etc). I think (but I'm not sure) this does NOT spawn a workitem in the threadpool. Only after the result is obtained (or an error), is the callback called from a new thread in the threadpool.

Is this assumption correct? Or is an asynchronous IO call, also under the covers just some thread which is spawned? An if this is the case, how can asynchronous calls be any better performant than spawning threads (using a threadpool) yourself and block?

also: how many asynchronous calls can one have being dealt with at any given time? In case of a threadpool being used, I guess as much as you like. But in case of IO asynchronous calls, is there a limit? And is so, how do you know what the limit is?

+6  A: 

Asynchronous IO is much more complicated thing than just using another thread from thread pool.

There are many different techniques inside OS, that support asynchronous IO:

1 Signaling a device kernel object

Not useful for performing multiple simultaneous I/O requests against a single device. Allows one thread to issue an I/O request and another thread to process it.

2 Signaling an event kernel object

Allows multiple simultaneous I/O requests against a single device. Allows one thread to issue an I/O request and another thread to process it.

3 Using alertable I/O

Allows multiple simultaneous I/O requests against a single device. The thread that issued an I/O request must also process it.

4 Using I/O completion ports

Allows multiple simultaneous I/O requests against a single device. Allows one thread to issue an I/O request and another thread to process it. This technique is highly scalable and has the most flexibility.

Sergey Teplyakov
@sergey: so you are saying there is indeed a difference. Then how many IO requests kan be pending at any one time? What is the sweetspot? Is this dependent on the IO targetted or something else? How can one know?
Toad
Simle answer:it depends. If you using IO Completion ports (this is the most complicated but most powerfull technique) OS would create as few thread as possible (depends on your hardware configuration) to handle all active IO tasks. Using alertable IO requires one thread for !every! pending IO operations. That's why IO coplection ports is much more effective and scalable: you can handle !many! simultanious IO requests with minimal resources. I think you should read "Windows via C/C++" or "Programming server-side application" by Jeffrey Richter to understand all of this. It's pretty tough stuff!
Sergey Teplyakov
@reinier The limits are based on everything from your number of CPUs to your Windows license key. But the limits are not important - as long as you use the asynchronous methods provided to you by the BCL, you *know* you will get better scalability than if you eg. `QueueUserWorkItem`.
bzlm
@sergey: great insight. I'm already using asynchronous IO operation where possible (in combination with the CCR which makes it more easy). But your story makes it less 'magical'. I'll be sure to read one of those books. thanks!
Toad