There's more...
The atomic reference counter guarantees correct behavior of shared_ptr across the threads, but you must remember that atomic operations are not as fast as non-atomic. shared_ptr touches the atomic variable on assignments, copy constructions, and on destruction of a non moved away shared_ptr. It means that on C++11 compatible compilers, you may reduce atomic operation's count using move constructions and move assignments where possible. Just use shared_ptr<T> p1(std::move(p)) if you are not going to use the p variable any more. If you are not going to modify the pointed value, it is recommended to make it const. Just add const to the template parameter of the smart pointer, and the compiler will make sure that you do not modify ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access