• C++中的RAII技法


    Resource Acquisition Is Initialization or RAII, is a C++ programming technique which binds the life cycle of a resource (allocated memory, thread of execution, open socket, open file, locked mutex, database connection—anything that exists in limited supply) to the lifetime of an object.

    所谓资源就是,一旦用了它,将来必须还给系统。如果不这样,糟糕的事情就会发生。常见的资源包括内存、文件描述符、互斥锁、数据库连接、网络sockets。

    以对象管理资源的两个关键想法:获得资源后立刻放进管理对象;管理对象运用析构函数确保资源被释放。

    对于heap-based资源,常被使的RAII classes分别是shared_ptrauto_ptr,有时可考虑boost::shared_arrayboost::scoped_array

    对于非heap-based资源,常使用RAII手法封装资源的取得与释放,这时需要考虑RAII对象被复制,常见的行为是禁制复制、施行引用计数法并指定删除器、使用深度拷贝复制底部资源、转移底部资源的拥有权。

    对于以上两种资源都需要考虑RAII对象析构时抛出异常的问题。

    示例代码如下。

    #include <mutex>
    #include <iostream>
    #include <string> 
    #include <fstream>
    #include <stdexcept>
    
    void write_to_file (const std::string & message) {
        // mutex to protect file access (shared across threads)
        static std::mutex mutex;
    
        // lock mutex before accessing file
        std::lock_guard<std::mutex> lock(mutex);
    
        // try to open file
        std::ofstream file("example.txt");
        if (!file.is_open())
            throw std::runtime_error("unable to open file");
        
        // write message to file
        file << message << std::endl;
        
        // file will be closed 1st when leaving scope (regardless of exception)
        // mutex will be unlocked 2nd (from lock destructor) when leaving
        // scope (regardless of exception)
    }
    

    参考:《Effective C++》、wikipediacppreference

  • 相关阅读:
    自制404页面
    Http错误代码含义
    数据访问基础类(基于Access数据库)
    NTILE函数在SQL Server 2000中的实现方法
    矩阵螺旋输出
    分区排名方案和排名值效率分析【图文+测试代码】
    安装eclipse axis2 插件(links 方式)
    perforce关闭服务后无法重启
    A4纸网页打印中对应像素的设定和换算
    2005数据库脚本在SQL2000上执行
  • 原文地址:https://www.cnblogs.com/shuaihanhungry/p/5796244.html
Copyright © 2020-2023  润新知