Re:Why asynchronous Rust doesn't work
by fulafel on Wednesday March 10, 2021 @02:43AM
(#26408851)
Are we talking about the same example?
No threads are spawned.
[ Parent
| on HN ]
`
fn do_work_and_then(func: fn(i32)) {
thread::spawn(move || {
// Figuring out the meaning of life...
thread::sleep_ms(1000); // gee, this takes time to do...
// ah, that's it!
let result: i32 = 42;
// let's call the `func` and tell it the good news...
func(result)
});
}
`you missed a snippet, a thread is spawned.
edit: formatting
Re:Why asynchronous Rust doesn't work
by imtringued on Wednesday March 10, 2021 @04:38AM
(#26409517)
If for some magic reason thread::sleep_ms(1000) takes longer than 2000ms the main function would reach its end and deallocate the closure that is about to get called. Basically use after free.
Re:Why asynchronous Rust doesn't work
by richardwhiuk on Wednesday March 10, 2021 @06:41AM
(#26410228)
And that's possible, because the sleep call is a suggestion, not a guarantee. If the CPU is blocked for 2s, then the order of waking the threads is undetermined, and the race will occur.
That is technically a race (sure) but at that point the program is getting killed anyway. Agree on the overall point though. I am a big fan of moving complete ownership / lifetime. It just makes things easier to reason about. In Chromium weak pointers are used to solve this use after free problem.
Re:Why asynchronous Rust doesn't work
by filleduchaos on Wednesday March 10, 2021 @03:05AM
(#26408963)
The `do_work_and_then` function does spawn a thread, it's one of the first things established in the article.