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

> Reference counted pointers themselves are usually not atomic, only the reference count updates are.

Fun way to start an argument where everybody is wrong: ask a group of C++ programmers whether std::shared_ptr is thread-safe.



Related talk from cppcon: https://youtu.be/lkgszkPnV8g


shared_ptr's thread safety is simple: it's perfectly thread-safe as long as each thread has its own copy. It of course does not provide any synchronization for the object it's pointing to.


Almost exactly one month ago I have a junior developer share the same shared_ptr across threads, where some threads could be writing to it by calling reset(). They forget that accessing the shared_ptr itself needs to be protected.

Rust prevents this problem completely though. You are either going to deal with Arc<Mutex<T>> or Mutex<Arc<T>>, depending on the situation (in this case the latter), and forgetting the Mutex will simply cause compilation failure.




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

Search: