Discuz! Board

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 175|回复: 0
打印 上一主题 下一主题

C++: RAII是什么——使用对象来管理资源

[复制链接]

1243

主题

2023

帖子

7771

积分

认证用户组

Rank: 5Rank: 5

积分
7771
跳转到指定楼层
楼主
发表于 2024-8-13 08:55:18 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
1. 什么是RAII
RAII是Resource Acquisition Is Initialization的缩写,即“资源获取即初始化”。它是C++语言的一种管理资源、避免资源泄漏的惯用法,利用栈的特点来实现,这一概念最早由Bjarne Stroustrup提出。在函数中由栈管理的临时对象,在函数结束时会自动析构,从而自动释放资源,因此,我们可以通过构造函数获取资源,通过析构函数释放资源。即:


Object() {
    // acquire resource in constructor
}

~Object() {
    // release resource in destructor
}

1
2
3
4
5
6
7
8
9
RAII总结如下:

将每一种资源封装在一个RAII类中:

所有资源在构造函数中获取,例如:分配内存、打开文件、建立数据库连接等;如果无法完成则在构造函数中抛出异常;
所有资源在析构函数中释放,例如:释放内存、关闭文件、销毁数据库连接等;不应该抛出任何异常。
通过RAII类实例获取资源:

具有自动生命管理周期或临时对象生命周期
其生命周期与第一种绑定。
2. 为什么要使用RAII
我们知道,在C++中,通过new运算符动态申请内存,例如:

Foo* ptr = new Foo(1);

// ...
delete ptr;
1
2
3
4
在这段代码中,new运算符在计算机内存的堆上申请了一块Foo类型的内存,然后将其地址赋值给存储在栈上的指针ptr。为了能够释放内存资源,我们需要使用完new运算符申请的内存后,手动调用delete运算符释放内存。

但是,情况并不总是如此简单。

Foo* ptr = new Foo(1);

f(ptr);  // -->① may throw exception
if(ptr->g()) {
    // ... --> ② may forget to delete ptr
    return;
}
// ...
delete ptr;
1
2
3
4
5
6
7
8
9
如上面这个例子,我们可能会遇到以下几种情况:

忘记delete释放内存。比如释放原指针指向的内存前就改变了指针的指向。
程序抛出异常后导致无法delete。比如上面的①处,如果f函数抛出异常,没有机会运行delete,从而导致内存泄漏。
需求变更后,修改了函数,新增了分支,提前返回,却没有delete;现实情况代码复杂的话可能没有这么显而易见。
而通过RAII这样一种机制,我们可以使其自动释放内存。

3. C++ STL中RAII的应用
3.1 智能指针
智能指针是RAII的一种实现,它是一种模板类,用于管理动态分配的对象。智能指针的主要作用是自动释放内存,从而避免内存泄漏。C++11中提供了三种智能指针:unique_ptr、shared_ptr和weak_ptr。它们的详细原理将在之后的文章中介绍。这里我们以unique_ptr为例,它的构造函数如下:

template< class T, class Deleter = std::default_delete<T> > class unique_ptr;
1
unique_ptr的析构函数会自动释放内存,因此,我们可以通过unique_ptr来管理动态分配的内存,从而避免内存泄漏。例如:

std::unique_ptr<int> ptr = std::make_unique<int>(1); // release memory when ptr is out of scope
1
3.2 互斥锁
在多线程编程中,std::lock_guard, std::unique_lock, std::shared_lock等也利用了RAII的原理,用于管理互斥锁。当这些类的等对象创建时,会自动获取互斥锁;当对象销毁时,会自动释放互斥锁。

std::lock_guard的构造函数如下:

template< class Mutex > class lock_guard;
1
std::lock_guard的析构函数会自动释放互斥锁,因此,我们可以通过std::lock_guard来管理互斥锁,从而避免忘记释放互斥锁。例如:


std::mutex mtx;
std::lock_guard<std::mutex> lock(mtx); // unlock when lock is out of scope
1
2
3
不使用RAII的情况下,我们需要手动释放互斥锁,如下所示:

std::mutex mtx;
mtx.lock();
// ...
mtx.unlock();
1
2
3
4
3.3 文件操作
std::ifstream, std:fstream等C++标准库的IO操作都是RAII的实现。

3.4 事务处理
数据库事务处理中,如果在事务结束时没有提交或回滚,就会导致数据库连接一直被占用,从而导致数据库连接池耗尽。因此,我们需要在事务结束时自动提交或回滚,从而释放数据库连接。这一过程也可以通过RAII来实现。

3.5 其他
RAII还可以用于管理其他资源,比如网络连接、线程等。

4. RAII的编程实践
基于RAII实现资源池的自动回收机制:

ResourcePool为资源池类,可以创建指定数量的资源,并提供获取和释放资源的接口。

ResourceWrapper为资源包装类,用于获取资源,并在对象销毁时自动释放资源。

Resource为资源类,用于模拟资源,通过id来标识,其构造函数和析构函数分别用于获取和释放资源。

实现资源管理类需要注意的一些事项:

需要仔细考虑拷贝构造函数和拷贝赋值运算符的实现,若需拷贝,应考虑实现引用计数或对资源进行深拷贝;若无必要,最好将其删除。这里我们使用了=delete进行了删除;

需提供移动构造函数和移动赋值运算符,以便于使用std::move(),转移资源的控制权;

提供获取原始资源的接口。

代码实现如下:

#include <iostream>
#include <vector>
#include <deque>
constexpr int kErrorId = -1;
template<typename T>
class ResourcePool {
public:
    ResourcePool(int size) {
        for (int i = 0; i < size; ++i) {
            pool_.emplace_back(i);
        }
    }

    T getResource() {
        if (pool_.empty()) {
            std::cout << "Resource is not available now." << std::endl;
            return T();
        }
        T resource = std::move(pool_.front());
        pool_.pop_front();
        std::cout<< "Resource " << resource.ID() << " is acquired." << std::endl;
        return resource;
    }

    void releaseResource(T&& resource) {
        if (resource.ID() == kErrorId) {
          return;
        }
        std::cout << "Resource " << resource.ID() << " is released." << std::endl;
        pool_.emplace_back(std::forward<T>(resource));
    }

private:
    std::deque<T> pool_;
};

template<typename T>
class ResourceWrapper {
public:
    ResourceWrapper(ResourcePool<T>& pool) : pool_(pool), resource_(pool_.getResource()) {
      if(resource_.ID() == kErrorId) {
        throw std::runtime_error("Resource is not available now.");
      }
    }
    ~ResourceWrapper() {
        pool_.releaseResource(std::move(resource_));
    }
    ResourceWrapper(const ResourceWrapper& other) = delete;
    ResourceWrapper& operator=(const ResourceWrapper& other) = delete;

    ResourceWrapper(ResourceWrapper&& other) noexcept : pool_(other.pool_), resource_(std::move(other.resource_)) {
    }

    ResourceWrapper& operator=(ResourceWrapper&& other) noexcept {
      pool_ = other.pool_;
      resource_ = std::move(other.resource_);
      return *this;
    }
    const T& GetRawResource() const noexcept{
        return resource_;
    }
private:
    ResourcePool<T>& pool_;
    T resource_;
};


class Resource {
public:
    constexpr explicit Resource(int id) : id_(id) {
      std::cout << "Resource " << id_ << " is created." << std::endl;
    }
    Resource(): id_(kErrorId) {}
    ~Resource() = default;
    int ID() const {
        return id_;
    }
    // delete copy constructor and copy assignment operator
    Resource(const Resource& other) = delete;
    Resource& operator=(const Resource& other) = delete;

    Resource(Resource&& other) noexcept : id_(other.id_) {
      other.id_ = kErrorId;
    }

    Resource& operator=(Resource&& other) noexcept {
      id_ = other.id_;
      other.id_ = kErrorId;
      return *this;
    }
private:
    int id_;
};

constexpr int kPoolSize = 3;
ResourcePool<Resource> pool(kPoolSize); // Resource pool with 3 resources in global scope.

void RequestRourceTest() {
    constexpr int kResourcesNum = 3;
    for (int i = 0; i < kResourcesNum; ++i) {
        ResourceWrapper<Resource> resource_wrapper(pool);
        resource_wrapper.GetRawResource();
    }
}

int main() {
    RequestRourceTest();
    return 0;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
运行输出结果如下:

Resource 0 is created.
Resource 1 is created.
Resource 2 is created.
Resource 0 is acquired.
Resource 0 is released.
Resource 1 is acquired.
Resource 1 is released.
Resource 2 is acquired.
Resource 2 is released.
1
2
3
4
5
6
7
8
9
5. 总结
在本文中,我们介绍了C++中的RAII技术,它是一种管理资源的方法,可以帮助我们避免内存泄漏和资源泄漏等问题。RAII技术的核心思想是将资源的获取和释放绑定在对象的生命周期中,这样可以确保资源在不再需要时被正确释放。我们还介绍了如何使用RAII技术来管理动态内存、文件句柄和互斥锁等资源,并提供了一些示例代码来说明如何实现RAII类。最后,我们还讨论了RAII技术的一些注意事项和最佳实践,以帮助开发人员编写更安全、更可靠的代码。希望本文能够帮助您更好地理解和应用RAII技术。

在本文的编程实践中,还使用了std::move()、std::forward()、noexcept等诸多现代C++技术,更多细节和不足之处,将在之后的文章中进行进一步探讨。
————————————————

                            版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

原文链接:https://blog.csdn.net/wsqiangz/article/details/134098624

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|firemail ( 粤ICP备15085507号-1 )

GMT+8, 2024-9-19 09:33 , Processed in 0.060099 second(s), 19 queries .

Powered by Discuz! X3

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表