Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> What does rust have to do with thread safety and race conditions? Is rust going to synchronize shared memory access for me?

Well, pretty close to that, actually! Rust will statically prevent you from accessing the same data from different threads concurrently without using a lock or atomic.

> what's preventing me from using C++ atomics to achieve the same thing

You might forget?



Rust will also prevent you from sharing the unsharable between threads.


The first comment is the definition of a data race, not preventing race conditions. And data races are trivial (sure, no static prevention in C++)


> 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).




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: