赞
踩
之前没阅读shared_ptr的代码,对它仅有的印象就是它是线程安全的。最近突然有同事告知,其他它不是线程安全的。于是,阅读分析了下源码。
源码中都是满满的模板类。
shared_ptr<_Tp>继承自__shared_ptr<_Tp>,其功能主要由父类__shared_ptr 实现。
__shared_ptr 中有两个模板定义的类型,第一个 Tp 就是 智能指针想保存的对象的类型。第二个Lp是加锁的策略 _Lock_policy,有三种策略:
// Available locking policies:
// _S_single single-threaded code that doesn't need to be locked.
// _S_mutex multi-threaded code that requires additional support
// from gthr.h or abstraction layers in concurrence.h.
// _S_atomic multi-threaded code using atomic operations.
默认使用 shared_ptr时,是没有传入这个策略的。这个策略的指定,是在编译期间,根据编译参数自行选择。
// Compile time constant that indicates
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。