Executive Summary
Any program may perform I/O operations in various forms like a file, a network , serial port or simple console operation. particularly in case of networking, sometimes individual I/O operation takes long time to complete. This poses its own kind of challenges: application development in managing CPU cycles and longer execution time etc. One option that comes to mind is to make a program multi-threaded and give the long running I/O operation to a thread. However, there is another solutions to the same problem and this is to perform I/O operations asynchronously.
There are few things to notice about asynchronous I/O over thread
- With Asynchronous code, it is known where the control will shift from one task to another; hence race conditions are harder to arrive. In case of multi-threaded program, the code needs to be safe to control race conditions. The I/O part of threaded code is comparatively easy but managing the shared state of critical section between threads (using locks/queues/etc) without race conditions is what makes it challenging. Using an asynchronous model means you have less activities going on at the same time so races can be easily avoided.
Project Highlights
With asynchronous approach, whole code shares the same process stack and it is kept small by continuos stack unwinding between tasks. Each thread will consume at least one memory page of stack, plus some unknown amount of memory for other data structures related to that thread’s state.