3 ms·
Threadsafe (no externally visible mutable data) singleton instance?
by e12e 9y ago
Threadsafe (no externally visible mutable data) singleton instance?
- jandrese 9y agoIsn't all data (outside of unsafe blocks) in Rust already threadsafe? Isn't that one of the big selling points of the language?
- wahern 9y agoRust isn't inherently thread-safe; not like in the way Java is thread-safe because of how the JVM's memory model is defined. Rust makes it really difficult to share mutable data. But once you do that, such as by smuggling a pointer _through_ an unsafe block, all bets are off, even for code outside an unsafe block. Also, as with Java there will always be bugs in the compiler and runtime. Rust programs were susceptible to StackClash just as much as C applications. No matter what language you use, if you minimize shared mutable data you'll minimize thread-safety issues.
- CrystalGamma 9y agoIf you use a C FFI in any language, including Java, all kinds of safety are off. Unsafe Rust is equivalent to C (with a lot of mandatory lints) in terms of safety, so Rust is not really less safe.
- alphaalpha101 9y agoNot necessarily. There is no actual reason that you couldn't be required to prove to the compiler that your code is safe. That proof might be parameterised by a proof that some external FFI function was safe, which you might not be able to actually prove and have to assume, but then you would have your assumptions well-documented. As it is, you have to justify the safety of your unsafe blocks to other programmers using comments, which kind of sucks. Still better than every other fast language in this area though so I can't complain much.
- jandrese 9y agoI get that all bets are off once an unsafe() block is in play, but in the intended use case it seems like it should be safe.
- kibwen 9y agoYou are correct there. Though we can quibble over the definition of "thread safety", concurrent memory safety is a subset of memory safety, which safe Rust enforces (or at least intends to enforce, modulo bugs).
- steveklabnik 9y agoIt is data-race free. That's one definition of "threadsafe", but there could be others; it's a pretty broad term.