0

I've read that using std::shared_ptr is bad practice, because it's slow, as it has ref_counter inside, which is atomic. Why there is no such smart pointer which is exactly like std::shared_ptr, but it is not synchronous, so ref_counter is not-thread-safe, but we will win some performance, as in some cases we don't need to care about thread-safety

Yeah, C++ do have std::unique_ptr but it doesn't count refs.

I know that we can omit this by passing ptr via const-ref, but it looks like a weird fix.

Bonny Cash
  • 36
  • 3
  • 2
    Related: [Is there a non-atomic equivalent of std::shared_ptr? And why isn't there one in ?](https://stackoverflow.com/q/15129263/555045) – harold Oct 11 '21 at 15:34
  • *"Why"*, because it was not proposed/accepted by committee. – Jarod42 Oct 11 '21 at 15:39
  • 4
    Avoiding `shared_ptr` in single-threaded code because of the atomic refcount operations is usually a form of premature optimization. What you really should be doing is avoiding `shared_ptr` because shared ownership is *usually* unnecessary. Then, if you need shared ownership, use `shared_ptr`. And finally, if profiling shows that the atomic refcount operations consume a substantial fraction of CPU time, drop in a thread-unsafe replacement. – Brian Bi Oct 11 '21 at 16:10

0 Answers0