Async Operation
Fact: true asynchrony must be provided at the OS (Operating System) level.
- CPU bound operations are inhenrently synchronous, hence they generally use Threading to support parallel operations. A thread can also pretend to be async by offloading it's taks to another thread.
- I/O bound operations generally use Asynchronous I/O
State Machine
Refer State Machine Design in C++
OS level Asynchrony
TBD
Application level Aysnchrony
Let's take an example of JavaScript's async/await
, which is a compiler-generated state machine
, that runs async functions, but relies on OS primitives (interrupts
, threads
) to acutally perform a task in async manner.
Often there won't be a new thread created to handle async operations. On Windows, the primary mechanism for performing async work is I/O Completion Ports - a Windows API, used under the hood even by the HttpClient
.
For non-I/O operations, we can always use Thread and ThreadPool API, to perform background work that will complete asynchronously.
How Async works?
TBD
Making a method `async` doesn't meant it will spawn a thread. I/O will not use a thread, unless intentionally created. This is managed at OS level.
General use of async is to help with I/O bound processes, not CPU bound. I/O has callback operated interfaces at the lowest level.
Internally the compiler makes a state machine, which it uses to pause/await/resume executing code.
The method basically turns into a state engine relinquishing control to the task scheduler waiting for a completed task being signalled.
There are no free lunches. To save on waiting on this network round-trip, the producer application has a
operation-stack or something
waiting to listen on the response, whenever it comes back. This means that part resources on the consumers will be occupied till that stack clears up, or for always serving some.
References
- There is no thread blog by Stephen Cleary
- Task.Run Etiquette and Proper Usage
- Task.Run Etiquette Examples
- Regarding
deadlocks
blog by Stephen Cleary - Parallel Computing - SynchronizationContext
- Async and Await
- Don't Block on Async Code
- Don't Block in Asynchronous Code
- Regarding
Task
blogs by Stephen Cleary - A Tour of Task, Part 0: Overview series
- Task.Run vs BackgroundWorker: Intro series
- Cancellation, Part 1: Overview series
- Asynchronous Messaging, Part 1: Basic Distributed Architecture series
- BackgroundService Gotcha: Startup series
- Talks by Stephen Cleary Search
- asyncfx
- Introduction to Async Streams
- Testing async code
- Stephen Toub
- An Introduction to System.Threading.Channels
- Asynchronous programming with async and await
- Multithreaded Asynchronous I/O & I/O Completion Ports blog by Dr Dobbs
- https://stackoverflow.com/questions/40916697/must-async-methods-be-supported-by-os-or-is-async-program-level-feature
- .NET Parallel Programming blogs
- Stephen Toub blogs