Overhead and implementation of using shared_ptr

前端 未结 2 1822
不知归路
不知归路 2021-02-19 07:12

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

2条回答
  •  名媛妹妹
    2021-02-19 07:28

    GCC's shared_ptr will use no locking or atomics in single-threaded code. In multi-threaded code it will use atomic operations if an atomic compare-and-swap instruction is supported by the CPU, otherwise the reference counts are protected by a mutex. On i486 and later it uses atomics, i386 doesn't support cmpxchg so uses a mutex-based implementation. I believe ARM uses atomics for the ARMv7 architecture and later.

    (The same applies to both std::shared_ptr and std::tr1::shared_ptr.)

提交回复
热议问题