4 ms·
For me it comes down to simplicity of the sharing model. With async you can basically ignore sharing and write stuff that works against the same data structures
by fnordpiglet 4y ago
For me it comes down to simplicity of the sharing model. With async you can basically ignore sharing and write stuff that works against the same data structures because you know they’re mutually exclusive. Once you move stuff into separate threads you need to be a lot more careful, using Arc and Mutex et al, or devise channel semantics. I tend to only use threads when I never want a future interfering with the scheduling of another future, either when one is really long or when low latency is absolutely critical. One example recently was I had very low latency network stuff happening on one thread and it was sending messages to a TUI thread. I absolutely didn’t want the networking code ever blocked by the TUI, even for short times.
I find it fascinating that LWT have come so far that this is a legitimate question. There was a time when async << threads << processes. But a lot of heroic good work went into bringing about this wonderful age.
- zokier 4y agoBut tokio is multithreaded, so you need to deal with all that complexity?
- brundolf 4y agoI believe its abstractions work the same way regardless of how many threads you do or don't give it
- insanitybit 4y agoThe multithreaded runtime requires that your futures be Send.
- pkolaczk 4y agoWhich isn't a big deal in most situations as you can still use mutable variables without mutexes, even across await points.
- mcronce 4y agoTokio has a single-threaded executor as well
- fnordpiglet 4y agoThat’s a runtime option, and I think it specifically matters when you’re using tasks. If you’re using network and io futures (I’m not sure of this) I don’t think it returns a thread backed future.
- CodesInChaos 4y ago> With async you can basically ignore sharing and write stuff that works against the same data structures because you know they’re mutually exclusive. How does that simplify things in rust? You don't get shared mutability either way, and RefCell is barely simpler than Mutex.
- pkolaczk 4y agoYou don't need RefCell. You can just use mut variables as long as you keep everything local inside one task. But one task can actually run many things concurrently, unlike a thread, so this is very powerful.