千家信息网

C++如何使用资源句柄自动管理资源并RAII

发表于:2026-11-03 作者:千家信息网编辑
千家信息网最后更新 2026年11月03日,小编给大家分享一下C++如何使用资源句柄自动管理资源并RAII,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!Reason(
千家信息网最后更新 2026年11月03日C++如何使用资源句柄自动管理资源并RAII

小编给大家分享一下C++如何使用资源句柄自动管理资源并RAII,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

Reason(原因)

To avoid leaks and the complexity of manual resource management. C++'s language-enforced constructor/destructor symmetry mirrors the symmetry inherent in resource acquire/release function pairs such as fopen/fclose, lock/unlock, and new/delete. Whenever you deal with a resource that needs paired acquire/release function calls, encapsulate that resource in an object that enforces pairing for you -- acquire the resource in its constructor, and release it in its destructor.

避免手动管理资源时发生泄露和复杂性。C++语言鼓励构造函数/析构函数的对称性映射资源确保/释放函数对中包含的本质的对称性。这些函数对包括fopen/fclose,lock/unlock,new/deletre等。无论什么时候,你处理一个需要成对调用申请/释放函数时,用一个强制进行成对操作的对象封装资源--在它的构造函数中申请资源,在它的析构函数中释放资源。

Example, bad(反面示例)

Consider(考虑如下代码):

void send(X* x, cstring_span destination)
{
auto port = open_port(destination);
my_mutex.lock();
// ...
send(port, x);
// ...
my_mutex.unlock();
close_port(port);
delete x;
}

In this code, you have to remember to unlock, close_port, and delete on all paths, and do each exactly once. Further, if any of the code marked ... throws an exception, then x is leaked and my_mutex remains locked.

在这段代码中,你必须记得在所有路径上unlock,close_port和delete,而且确切地只做一次。另外,如果任何一处标有...的代码抛出了异常,那么x就会泄露,my_mutex会保持锁定。

Example(示例)

Consider(考虑):

void send(unique_ptr x, cstring_span destination)  // x owns the X
{
Port port{destination}; // port owns the PortHandle
lock_guard guard{my_mutex}; // guard owns the lock
// ...
send(port, x);
// ...
} // automatically unlocks my_mutex and deletes the pointer in x

Now all resource cleanup is automatic, performed once on all paths whether or not there is an exception. As a bonus, the function now advertises that it takes over ownership of the pointer.

现在所有的资源清除都是自动的,在每条路径上执行一次。无论是否存在异常。作为额外的奖励,这个函数对外宣称它负责资源的所有权。

What is Port? A handy wrapper that encapsulates the resource:

什么是Port?一个封装资源的便利的容器。

class Port {
PortHandle port;
public:
Port(cstring_span destination) : port{open_port(destination)} { }
~Port() { close_port(port); }
operator PortHandle() { return port; }

// port handles can't usually be cloned, so disable copying and assignment if necessary
Port(const Port&) = delete;
Port& operator=(const Port&) = delete;
};
Note(注意)

Where a resource is "ill-behaved" in that it isn't represented as a class with a destructor, wrap it in a class or use finally

当资源由于没有表现为一个带有虚构函数的类而存在"病态行为",用一个类封装它或者使用finally。

以上是"C++如何使用资源句柄自动管理资源并RAII"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!

资源 函数 C++ 管理 代码 篇文章 封装 句柄 对称 内容 对称性 示例 路径 复杂 不怎么 原因 反面 复杂性 大部分 容器 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 伟宏互联网科技 互联网科技产业 江苏根荣互联网科技 科技互联网公司装修 互联网核心科技是什么 河南真二互联网科技有限公司 科技互联网技术 互联网科技与金融科技 互联网 科技 睿民互联网科技 碳银互联网科技有限公司 创业天下互联网科技有限公司 以后互联网后是啥科技 互联网科技创新服务 湖北码尼互联网科技有限公司电话 洛阳昕诺飞互联网科技有限公司 工业互联网山东科技大学 张召忠谈中国互联网科技 互联网高科技行业有哪些企业 唐山双旺合互联网科技产业园 中关村互联网科技集团 互联网科技活动banner 科技互联网公司装修 互联网巨头反垄断金融科技 现代科技和互联网有关系吗 深圳育鹏互联网科技有限公司招聘 黑科技 体验场景 互联网大会 广州百诚互联网科技有限公司官网 宁波跨境商互联网科技有限公司 全球科技与互联网公司市值变化
0