首页 > 其他分享 >何时/如何使用 std::enable_shared_from_this<T>?

何时/如何使用 std::enable_shared_from_this<T>?

时间:2024-06-17 16:23:43浏览次数:11  
标签:std enable Processor shared data ptr

要点回顾


  • 继承自 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_thisstd::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_this allows an object t that is currently managed by a std::shared_ptr named pt to safely generate additional std::shared_ptr instances pt1, 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 function shared_from_this. If an object t of type T is managed by a std::shared_ptr<T> named pt, then calling T::shared_from_this will return a new std::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"


为什么需要 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" 内部


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,Processor,shared,data,ptr
From: https://www.cnblogs.com/Steven-HU/p/18252632

相关文章

  • 【类脑计算】突触可塑性模型之Hebbian学习规则和STDP
    1引言突触可塑性(Synapticplasticity)指经验能够修改神经回路功能的能力。特指基于活动修改突触传递强度的能力,是大脑适应新信息的主要调查机制。分为短期和长期突触可塑性,分别作用于不同时间尺度,对感官刺激的短期适应和长期行为改变及记忆存储至关重要。非对称ST......
  • 【知识点】std::thread::detach std::lock_guard std::unique_lock
    在C++11中,std::thread提供了并发编程的基础设施,使得我们可以创建和管理线程。std::thread的detach方法是一种常用的线程管理方式,允许线程在后台独立运行,而不必与主线程同步或等待其完成。std::thread::detach方法当你调用std::thread对象的detach方法时,线程将......
  • mptcp inside lxc container can't access /proc/sys/net/mptcp_enabled
    https://github.com/multipath-tcp/mptcp/issues/470 VenkateswaranJ commented onMar20,2022 • edited Hi,Ihavecreatedanlxccontainer(ubuntu20.04)withprivilegedmodeandmyhostmachinehasmptcpkernelinstalledubuntu20.04.For......
  • gcc编译时报错 fatal error: stdio.h: 没有那个文件或目录
    在kylinV10中使用GCC编译代码时遇到如下问题:首先确认了,自己单词没有拼写错。然后再检查GCC的版本,确实没问题。没有标准的头文件需要安装build-essential来解决。需要安装build-essential。执行以下命令:sudoapt-getinstallbuild-essential如无兼容版本可使用可使用ap......
  • Rustdesk 自建服务器
    自建rustdesk远程终端服务器,解决稳定远程控制需求。1、购买腾讯云服务器,99一年2、修改登录终端用户,默认使用ubuntu账户登录,存在权限限制,不能够上传文件Ubuntu系统如何使用root用户登录实例?(https://cloud.tencent.com/document/product/213/17278#ubuntu-.E7.B3.BB.E7.......
  • QWidget 属性——enabled
    ......
  • 对#include <stdio.h>的简单理解
    stdio.h是C语言中的一个文件,文件名是stdio,拓展名是.h,就像.exe和.jpg一样。当然.h是C语言的文件类型。stdio原意是standardinput&output,是标准输入和输出。标准输入函数是printf(),标准输出函数是scanf(),如果你写的程序中用到了这两个函数,就需要#include<stdio.h>,没用到则......
  • RustDesk 搭建
    Web、API部署教程:https://www.52pojie.cn/thread-1708319-1-1.htmlRustDesk服务端下载:https://github.com/rustdesk/rustdesk-server/releasesRustDesk客户端下载:https://github.com/rustdesk/rustdesk/releases/tag/1.2.3-2RustDesk官方部署教程:https://rustdesk.com/docs......
  • 错误处理:fmt::Display & std::error::Error
    错误处理为什么要给错误类型(如JsonError)实现fmt::Displaytrait?在Rust中,fmt::Displaytrait允许你定义一个类型如何被格式化为人类可读的字符串。这通常用于错误信息、日志记录或任何其他用户输出。实现fmt::Display需要定义fmt函数,该函数写入特定格式的数据......
  • LNMP 环境下使用 Zstd 压缩优化网站备份脚本
    网站的备份一直都是网站运营、服务器运维中很重要的一环,明月无论是在自己的服务器还是客户的代运维服务器上都是非常重视网站备份的,尤其热衷于优化网站备份这块儿,毕竟明月自己的服务器配置一直都是最低的1H1G呀,就这配置常年都是4-5个网站放着呢!明月的博客毕竟有十来年了,单......