Not if you use the return value. The return value itself is temporary, whose lifetime is beyond the scope of the function; This will be destroyed at the end of the full expression that calls A::clone_a . Therefore, if you write something like:
shared_ptr<int> newA = object->clone_a();
formal semantics will be to temporarily return object->clone_a() to copy to newA , in the context of the caller (and therefore not protected by the mutex). In this particular case, you may get away from it because of the RVO, but it will not necessarily be and there are other cases when the RVO cannot intervene.
If all that bothers you is a copy of the pointer, I am sure that if you set the correct compiler options ( -D somthing ), boost::shared_ptr will behave atomically. In this case, you donโt need a mutex at all.
source share