Comment by astrange
9 months ago
Async/await isn't related to threading (although many users and implementations confuse them); it's a way of transforming a function into a suspendable state machine.
9 months ago
Async/await isn't related to threading (although many users and implementations confuse them); it's a way of transforming a function into a suspendable state machine.
Games need async/await for two main reasons:
- coding multi-frame logic in a straightforward way, which is when transforming a function into a suspendable state machine makes sense
- using more cores because you're CPU-bound, which is literally multithreading
Both cases can be covered by other approaches, though:
- submitting multi-frame logic as job parameters to a separate system (e.g., tweening)
- using data parallelism for CPU-intensive work
I know. But threading, and earlier processes, were less scalable but potentially faster ways of handling concurrent requests.
It's also much easier to reason about, since scheduling is no longer your problem and you can just write sequential code.
That's one way to see it. But the symmetric view is equally valid: async await is easier to reason about because you see were the block points are instead of having to guess which function is blocking or not.
In any case you aren't writing sequential code, it's still concurrent code, and there's a trade-off between the writing simplicity of writing it as if it was sequential code, and the reading simplicity of having things written down explicitly.
This “write-time vs read-time” trade of is everywhere in programming BTW, that's also the difference between error-as-return-values and exception, or between dynamic typing and static one for instance.