Re:There's No Such Thing as “Implicitly Atomic”
by Georgelemental on Thursday October 05, 2023 @03:24PM
(#37783068)
Will prevent data races, yes.
Re:There's No Such Thing as “Implicitly Atomic”
by _nalply on Thursday October 05, 2023 @03:50PM
(#37783418)
Of course I meant data races. Or am I barking up the wrong tree?
And of course only if you use safe Rust.
Re:There's No Such Thing as “Implicitly Atomic”
by kevincox on Thursday October 05, 2023 @03:59PM
(#37783532)
Yes.
It is actually fairly tricky to get a value across threads like this. The simplest I could come up with is this:
struct UnsafeSync<T>(T);
unsafe impl<T> Sync for UnsafeSync<T> {}
fn main() {
let i = std::sync::Arc::new(UnsafeSync(std::cell::Cell::new(0)));
let thread_i = i.clone();
std::thread::spawn(move || {
thread_i.0.set(1);
});
eprintln!("i is {}", i.0.get());
}
https://play.rust-lang.org/?version=stable&mode=debug&editio...
Re:There's No Such Thing as “Implicitly Atomic”
by _nalply on Thursday October 05, 2023 @04:12PM
(#37783698)
Why didn't you use a Mutex?
Re:There's No Such Thing as “Implicitly Atomic”
by kevincox on Thursday October 05, 2023 @04:16PM
(#37783744)
What are you thinking?
I could have used a Mutex then cast the `&Mutex` to `&mut Mutex` so that I could call `.get_mut()`. But this didn't seem simpler.
Re:There's No Such Thing as “Implicitly Atomic”
by _nalply on Thursday October 05, 2023 @04:34PM
(#37783948)
I wanted to avoid unsafe.
Because unsafe introduces conceptual complications and it seemed to be simpler to avoid that in an example.
Right?