何时/如何使用 std::enable_shared_from_this<T>?
要点回顾
- 继承自
std::enable_shared_from_this<T>的类能够在其自身实例中通过std::shared_from_this方法创建一个指向自己的std::shared_ptr<T>智能指针。 - 从一个裸指针创建多个
std::shared_ptr<T>实例会造成严重的后果,其行为是未定义的。 std::enable_shared_from_this<T>实际包含了一个用于指向对象自身的std::weak_ptr<T>指针。
引言
本文介绍 std::enable_shared_from_this 及 std::shared_from_this 的基本概念和使用方法。
定义 std::enable_shared_from_this
以下内容是 cppreference.com 上关于 std::enable_shared_from_this 的定义和说明:
Defined in header < memory >
template< class T > class enable_shared_from_this; (since C++11)
std::enable_shared_from_thisallows an object t that is currently managed by astd::shared_ptrnamed ptto safely generate additional std::shared_ptr instancespt1, pt2, ... that all share ownership of t with pt.Publicly inheriting from
std::enable_shared_from_this<T>provides the type T with a member functionshared_from_this. If an object t of type T is managed by astd::shared_ptr<T>named pt, then callingT::shared_from_thiswill return a newstd::shared_ptr<T>that shares ownership of t with pt.
简单来说就是,继承自 std::enable_shared_from_this<T> 的类能够在其自身实例中通过 std::shared_from_this 方法创建一个指向自己的 std::shared_ptr<T> 智能指针。
想要理解 std::enable_shared_from_this<T>,首先得知道为什么需要 std::enable_shared_from_this<T>,请看下文。
使用 std::enable_shared_from_this<T>
为什么需要 std::enable_shared_from_this<T>? 我们从一个例子讲起,会更容易一些。
假定有一个类 Processor, 它的作用是异步处理数据并且存储到数据库。当 Processor 接收到数据时,它通过一个定制的 Executor 类型来异步处理数据:
class Executor {
public:
//Executes a task asynchronously
void
execute(const std::function<void(void)>& task);
//....
private:
/* Contains threads and a task queue */
};
class Processor {
public:
//...
//Processes data asynchronously. (implemented later)
void processData(const std::string& data);
private:
//Called in an Executor thread
void doProcessAndSave(const std::string& data) {
//1. Process data
//2. Save data to DB
}
//.. more fields including a DB handle..
Executor* executor;
};
Client 类包含了一个 std::shared_ptr<Processor> 实例,Processor 从 Client 类接收数据:
class Client {
public:
void someMethod() {
//...
processor->processData("Some Data");
//..do something else
}
private:
std::shared_ptr<Processor> processor;
};
以上示例中,Executor 是一个线程池,用于执行任务队列中的任务。
在 Processor::processData 中,我们需要传递一个(指针)函数(lambda 函数)给 Executor 来执行异步操作。该 lambda 函数调用 Processor::doProcessAndSave 以完成实际的数据处理工作。因此,该 lambda 函数需要捕获一个 Processor 对象的引用/指针。我们可以这样做:
void Processor::processData(const std::string& data) {
executor->execute([this, data]() { //<--Bad Idea
//Executes in an Executor thread asynchronously
//'this' could be invalid here.
doProcessAndSave(data);
});
}
然而,因为种种原因,Client 可能会随时重置 std::shared_ptr<Processor>,这可能导致 Processor 的实例被析构。因此,在执行 execute 函数时或者在执行之前,上述代码中捕获的 this 指针随时可能会变为无效指针。
怎么办?
我们可以通过在 lambda 函数中捕获并保留一个指向当前对象本身的 std::shared_ptr<Processor> 实例来防止 Processor 对象被析构。
下图展示了示例代码的交互逻辑:

那么,在 Processor 实例中通过 shared_ptr(this) 创建一个智能指针呢?其行为是未定义的!
std::shared_ptr<T> 允许我们安全地访问和管理对象的生命周期。多个 std::shared_ptr<T> 实例通过一个共享控制块结构(a shared control block structure)来管理对象的生命周期。一个控制块维护了一个引用计数,及其他必要的对象本身的信息。
void good() {
auto p{new int(10)}; //p is int*
std::shared_ptr<int> sp1{p};
//Create additional shared_ptr from an existing shared_ptr
auto sp2{sp1}; //OK. sp2 shares control block with sp1
}
从一个裸指针创建一个 std::shared_ptr<T> 会创建一个新的控制块。从一个裸指针创建多个 std::shared_ptr<T> 实例会造成严重的后果:
void bad() {
auto p{new int(10)};
std::shared_ptr<int> sp1{p};
std::shared_ptr<int> sp2{p}; //! Undefined Behavior
}
因此,我们需要一个机制能够达到我们的目的(捕获并保留一个指向当前对象本身的 std::shared_ptr<Processor> 实例)。
这就是 std::enable_shared_from_this<T> 存在的意义,以下是修改后的类 Processor 实现:
class Processor : public std::enable_shared_from_this<Processor> {
//..same as above...
};
void Processor::processData(const std::string& data) {
//shared_from_this() returns a shared_ptr<Processor>
// to this Processor
executor->execute([self = shared_from_this(), data]() {
//Executes in an Executor thread asynchronously
//'self' is captured shared_ptr<Processor>
self->doProcessAndSave(data); //OK.
});
}
self = shared_from_this() 传递的是一个合法的 std::shared_ptr<Processor> 实例,合法的类 Processor 对象的引用。
深入 std::enable_shared_from_this<T> 内部
std::enable_shared_from_this<T> 的实现类似:
template<class T>
class enable_shared_from_this {
mutable weak_ptr<T> weak_this;
public:
shared_ptr<T> shared_from_this() {
return shared_ptr<T>(weak_this);
}
//const overload
shared_ptr<const T> shared_from_this() const {
return shared_ptr<const T>(weak_this);
}
//..more methods and constructors..
//there is weak_from_this() also since C++17
template <class U> friend class shared_ptr;
};
enable_shared_from_this 包含了一个 std::weak_ptr<T> 指针,这正是函数 shared_from_this 返回的内容。注意,为什么不是 std::shared_ptr<T>? 因为对象包含自身的计数引用会导致对象永远不被释放,从而发生内存泄漏。上述代码中 weak_this 会在类对象被 std::shared_ptr<T> 引用时赋值,也就是std::shared_ptr<T> 实例的构造函数中赋值,这也是为什么类 enable_shared_from_this 的最后,其被声明成为了 shared_ptr 的友元。
总结
到此,关于 std::enable_shared_from_this<T> 的介绍就结束了。
引用
https://en.cppreference.com/w/cpp/memory/enable_shared_from_this
https://www.nextptr.com/tutorial/ta1414193955/enable_shared_from_this-overview-examples-and-internals
何时/如何使用 std::enable_shared_from_this<T>?的更多相关文章
- 为什么使用enable_shared_from_this——shared_ptr两类错误
在使用C++实现弱回调时,订阅者应当维护一系列发布者的weak_ptr,而发布者注册回调时要传出this的shared_ptr指针,流行的实现方法是使用std::enable_shared_from_ ...
- enable_shared_from_this类的作用和实现
使用举例 实际中, 经常需要在一个被shared_ptr管理的对象的内部获取自己的shared_ptr. 比如: 通过this指针来构造一个shared_ptr, 如下: struct Bad { v ...
- 如何用enable_shared_from_this 来得到指向自身的shared_ptr 及对enable_shared_from_this 的理解
在看<Linux多线程服务端编程:使用muduo C++网络库> 的时候,在说到如何防止在将对象的 this 指针作为返回值返回给了调用者时可能会造成的 core dump.需使用 ena ...
- C++11新特性之十:enable_shared_from_this
enable_shared_from_this是一个模板类,定义于头文件<memory>,其原型为: template< class T > class enable_shar ...
- std::weak_ptr
weak_ptr 是一种不控制对象生命周期的智能指针, 它指向一个 shared_ptr 管理的对象. 进行该对象的内存管理的是那个强引用的 shared_ptr. weak_ptr只是提供了对管理对 ...
- enable_shared_from_this
头文件<memory> enable_shared_from_this是一个模板类. 使用场景:需要把自己类对象作为参数传给其他函数时,就需要传递一个指向自身的share_ptr. str ...
- enable_shared_from_this用法分析
一.背景 在为什么需要异步编程文章末尾提到,"为了使socket和缓冲区(read或write)在整个异步操作的生命周期一直保持活动,我们需要采取特殊的保护措施.你的连接类需要继承自enab ...
- C++11新特性:enable_shared_from_this
enable_shared_from_this是一个模板类,定义于头文件<memory>,其原型为:template< class T > class enable_share ...
- C++11 新特性:enable_shared_from_this
enable_shared_from_this是一个模板类,定义于头文件<memory>,其原型为:template< class T > class enable_share ...
- C++11笔记<一>
目录: 1.std::share_ptr智能指针: 2.std::tr1::function模板类: 3.stringstream: 4.set/vector/map: 5.static_cast&l ...
随机推荐
- [FE] Quasar 性能优化: 减小 vendor.js 尺寸
默认情况下,出于性能和缓存的原因,Quasar 所有来自 node_modules 的东西都会被注入到 vendor 中. 但是,如果希望从这个 vendor.js 中添加或删除某些内容,可以如下这样 ...
- [Blockchain] 以太坊主流测试网 ropsten 和 kovan 的区别 以及 如何选择
ropsten 采用 POW (Proof-of-Work)共识机制,挖矿难度系数非常低,容易被攻击,不够低碳环保. kovan 采用 POA (Proof-of-Authority)共识机制,不需要 ...
- WPF 通过 GetMessageExtraInfo 方法获取当前收到的鼠标消息是否由触摸转换过来
本文将告诉大家如何在 WPF 或者其他 Win32 应用里面,在收到鼠标消息时,通过 GetMessageExtraInfo 方法获取当前收到的鼠标消息是否由触摸消息提升而来 大家都知道,在不开启 W ...
- C++多态与虚拟:C++编译器对函数名的改编(Name Mangling)
如果函数名称都相同(也就是被overloaded),编译器在面对你的函数唤起动作时,究竟是如何确定调用哪个函数实体呢?事实上,编译器把所有同名的overloaded functions视为不同的函数, ...
- Golang 之 casbin(权限管理)
目录 1. 权限管理 官网 编辑器测试 1.1.1. 特征 Casbin的作用 Casbin不执行的操作 1.1.2. 怎么运行的 1.1.3. 安装 1. 示例代码 xormadapter 2. 示 ...
- JS实现下拉框切换和tab标签切换
现在商城网页上会有下拉框切换内容,是如何实现的呢,研究了一天,在调整js代码和查找bug.最终完成了自己想要的效果,我没有写CSS样式,只是实现了基本功能,如果对你有所帮助,可以自己写css,使其更加 ...
- jquery中.html(),.text()和.val()的差异总结
.html(),.text(),.val()三种方法都是用来读取选定元素的内容: .html()是用来读取元素的html内容(包括html标签): .text()用来读取元素的纯文本内容,包括其后代元 ...
- 说一下flex的属性
flex-grow项目的放大比例,默认为0,即如果存在剩余空间,也不放大. flex-shrink属性定义了项目的缩小比例,默认为1,即如果空间不足,该项目将缩小.负值对该属性无效. flex-bas ...
- Splashtop 支持对 Adobe Creative Cloud 视频应用程序的远程访问
快讯:Splashtop 为 Adobe Creative Cloud 视频和音频产品的用户提供了对其工作站的高性能远程访问,使他们能够在家中工作. 程访问和远程支持解决方案的领导者 Splashto ...
- Lua热更学习--使用toLua中的协程
[6] C#访问调table类中的成员变量和函数 访问table中的变量和函数 lua中可以使用table作为class,因此对table中的函数访问调用是必要的根据前面对table访问和functi ...