Implicit async/await #5469
-
I wonder why it has been decided to introduce the Why can't the runtime just figure out when there's some I/O going on and implicitly implement the state machine? What's the advantage of explicitly deciding when and how to implement or use an async API? Also, why wouldn't you want to use the async way? I'm probably missing a lot of background so I'm looking forward to informative replies. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
The C# team wanted there to be a very clear delineation for asynchronous boundaries, both because of the overhead of the state machine and because you are likely to switch between threads which can have a direct impact on how your code behaves. Anything thread-affine, like thread locals, locking, etc., effectively break in these cases, and without compiler support and understanding it would be very easy to write code that relies on these features that will not work correctly at runtime. If you want a different perspective on how async can be implemented in a language you can check out OpenJDK Loom, which aims to bring user-mode threads to the Java language. It does sound enticing but there are a lot of caveats with this design. It makes the giant assumption that the runtime has enough control over the ways to perform blocking I/O that can be rewritten to support parking the user-mode thread, and the list of cases that aren't supported is a lot longer than you might think. For example, In the C# world, which has a much wider and diverse ecosystem and encourages the mixing of native code, I think something like user-mode threading would be untenable. Something like |
Beta Was this translation helpful? Give feedback.
-
The duplication part can be resolved using https://github.com/zompinc/sync-method-generator |
Beta Was this translation helpful? Give feedback.
The C# team wanted there to be a very clear delineation for asynchronous boundaries, both because of the overhead of the state machine and because you are likely to switch between threads which can have a direct impact on how your code behaves. Anything thread-affine, like thread locals, locking, etc., effectively break in these cases, and without compiler support and understanding it would be very easy to write code that relies on these features that will not work correctly at runtime.
If you want a different perspective on how async can be implemented in a language you can check out OpenJDK Loom, which aims to bring user-mode threads to the Java language. It does sound enticing but ther…