3 ms·
> data races are trivial Imagine this C++ code: class Foo { // ... public: void frobFoo(); }; Now, is it okay to call `frobFoo` from multiple thr
by School-Cotton 1y ago
> data races are trivial
Imagine this C++ code:
class Foo {
// ...
public:
void frobFoo();
};
Now, is it okay to call `frobFoo` from multiple threads at once? Maybe, maybe not -- if it's not documented (or if you don't trust the documentation), you will have to read the entire implementation to answer that.
Now imagine this Rust code:
struct Foo {
// ...
}
impl Foo {
fn frobFoo(&mut self) {
// ...
}
}
Now, is `frobFoo` okay to call from multiple threads at once? No, and the language will automatically make it impossible to do so.
If we had `&self` instead of `&mut self`, then it might be okay, you can discover whether it's okay by pure local reasoning (looking at the traits implemented by Foo, not the implementation), and if it's not then the language will again automatically prevent you from doing so (and also prevent the function from doing anything that would make it unsafe).