2 ms·
Sure, you can be preempted by another thread (or even process) even within an async function, so why should it matter? The borrow checker should still prevent a
by gpderetta 5y ago
Sure, you can be preempted by another thread (or even process) even within an async function, so why should it matter? The borrow checker should still prevent anything bad from happening.
I'm not a rust programmer, but personally I think that stackless coroutines are probably a necessary evil in system languages as full stack switching might have too much overhead. But let's stop pretending that it is actually a good thing.
Still I think that even with stackless coroutines await should be the default (the async keyword would be used if you explicitly want the promise), which, in addition to better ergonomics, would allow the async-ness of a function to be inferred, possibly as a function of its type parameters. This at the very least would avoid duplicating a lot of functionality.
- jayd16 5y ago>Sure, you can be preempted by another thread (or even process) even within an async function, so why should it matter? You misunderstand. Its not about whether your thread can be pre-empted, its that your logical execution can hold exclusive access to a thread. There are common design patterns that use exclusive threads as a means of synchronization.
- gpderetta 5y agoI understand. What I'm saying is that marking async points explicitly is neither necessary nor sufficient to guarantee exclusive use of the thread. The borrow checker or some other explicit critical section markers are a better soluiton.
- roblabla 5y ago> Sure, you can be preempted by another thread (or even process) even within an async function That really depends on the platform. Async rust is also being leveraged to build RTOS on embedded platform for instance. If async rust was preemptive by default, that would be impossible. It's not a matter of performance there, but of correctness.
- Matthias247 5y agoIt would actually be very hard to build a RTOS using async functions - and the current use of async/await on embedded Rust is indeed not about RTOS, it's just embedded code. The reason for that is that the point of a RTOS is providing guarantees: A certain task should be guaranteed to run within a certain time. To provide that guarantees, the RTOS needs to support preemption and prioritization. That allows it to schedule a certain task even if another task is running for too long. I'm not aware about any RTOS which makes use of cooperative multitasking.
- jlokier 5y ago> I think that stackless coroutines are probably a necessary evil in system languages as full stack switching might have too much overhead. I think it's worth noting that the big operating system kernels - perhaps the ultimate target of "system languages" - tend to use stackful coroutines rather than stackless. Stack switching tends to be similar speed overhead than stackless, despite surface appearances in how the code may appear. Stackless tends to do more memory allocation and freeing, though.
- gpderetta 5y agoSure, but the perceived advantage of stackless coroutines is not faster switching time, but lower peak memory usage. OSs do not run 100s of thousands of tasks as threads, and when they do they use hand written state machines.