c++ - Overhead and implementation of using shared_ptr -
short introduction: working on multithread code , have share dynamically allocated objects between 2 threads. make code cleaner (and less error-prone) want explicitly "delete" objects in each thread , that's why want use shared_ptr.
first question:
i want know if implementation of -> operator in shared_ptr has overhead (e.g. larger unique_ptr) during run time. objects talking longlife instances copied once after creation (when distribute them between threads), access these objects' methods , fields.
i aware, shared_ptr protect reference counting.
second question:
how shared_ptr optimized in libstdc++? use mutex or take advantage of atomic operations (i focus on x86 , arm platforms)?
first question: using
operator->
all implementations have seen have local cache of t* right in shared_ptr<t> class field on stack, operator-> has comparable cost using stack local t*: no overhead @ all.
second question: mutex/atomics
i expect libstdc++ use atomics on x86 platform, whether through standard facilities or specific g++ intrinsics (in older versions). believe boost implementation did so.
i cannot, however, comment on arm.
note: c++11 introducing move semantics, many copies naturally avoided in usage of shared_ptr.
note: read correct usage of shared_ptr here, can use references shared_ptr (const or not) avoid of copies/destruction in general, performance of not important.
Comments
Post a Comment