Short introduction: I am working on multithread code and I have to share dynamically allocated objects between two threads. To make my code cleaner (and less error-prone) I want to explicitly "delete" objects in each thread and that's why I want to use shared_ptr.
First question:
I want to know if implementation of -> operator in shared_ptr has some extra overhead (e.g. larger then unique_ptr) during run time. Objects I am talking about are usually longlife instances copied only once after creation (when i distribute them between threads), then I only access these objects' methods and fields.
I am aware, that shared_ptr only protect reference counting.
Second question:
How well are shared_ptr optimized in libstdc++? Does it always use mutex or take advantage of atomic operations (I focus on x86 and ARM platforms)?
shared_ptr, there should be zero overhead when dereferencing the pointer via->. I am not familiar with libstdc++, so I cannot answer your second question. You have the headers, though, so you can easily find out by taking a look at how it's implemented.std::atomic<int>or something like that for the reference counter; whether that's a true hardware (lockfree) atomic depends on the compiler version -- I believe this was improved in GCC 4.7.0.operator->looks exactly the same as the one of good oldauto_ptr, i.e. can be expected to be zero overhead.shared_ptr, and there are many versions of GCC and libstdc++. Which one are you talking about?