复制shared_ptr线程安全问题

hittyo hittyo 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区


shared_ptr文档中有这么一段:

shared_ptr objects offer the same level of thread safety as built-in types. A shared_ptr instance can be
"read " (accessed using only const operations) simultaneously by multiple threads. Different shared_ptr instances can be
"written to " (accessed using mutable operations such as operator= or reset) simultaneosly by multiple threads (even when these instances are copies, and share the same reference count underneath.)
Any other simultaneous accesses result in undefined behavior.

这意味着复制使用没有问题,但我后来又发现如下问题:

shared_ptr在传参以及operator=操作时会对记数器进行增加,但是记数器本身不是线程安全,这就非常费解了。

难道在shared_ptr当参数传递时必须要用到weak_ptr来引用?
...全文

相关推荐

评论 12
收藏 已收藏
分享
返回顶部
hittyo
C++ 语言 65,214 成员 250,514 内容