why is std::shared_ptr using atomic cpu operations

后端 未结 2 1955
礼貌的吻别
礼貌的吻别 2021-02-08 02:35

I have a problem understanding why shared_ptr is using atomic cpu instructions... I cant figure out the reasons because it is NOT thread safe. Can somebody please explain.

2条回答
  •  一生所求
    2021-02-08 03:28

    Any instance of shared_ptr is multi-thread safe. The data it points to is not multi-thread safe. See this.

    The atomic instructions, if properly applied (protection done in the same order by competing thread access) is one way to implement thread safety. Another way is by use of mutexes.

    See a similar question for BOOST: Is boost shared_ptr xxx thread safe?

提交回复
热议问题