Never snooze a future
jacko.io
jacko.io
For the first example the better option would be to use the `race` operation [2]. For the second it would be better to use `ConcurrentStream` [3]. Many (but not all) of the issues people have with cancellation in async Rust can be improved with the advice: "don't use select!".
[1]: https://blog.yoshuawuyts.com/futures-concurrency-3/#issues-w...
[2]: https://docs.rs/futures-concurrency/latest/futures_concurren...
[3]: https://docs.rs/futures-concurrency/latest/futures_concurren...
So maybe the lesson I'd take is that if you're programming with locks (or other synchronization primitives), your concurrency model has to be preemptive. Cooperative concurrency + locks = invitation to very subtle bugs.
Wait, what would be the point of using locks then? It seems to me there's no point taking a lock if you're gonna release it without calling any awaits, because nothing can interfere anyway. Or do you mean cases where you have both cooperative and preemptive concurrency in the same program?
This is probably true only in single threaded executor. Other threads often exist.