您好,登錄后才能下訂單哦!
小編給大家分享一下C++如何使用資源句柄自動管理資源并RAII,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
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> x, cstring_span destination) // x owns the X
{
Port port{destination}; // port owns the PortHandle
lock_guard<mutex> 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;
};
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”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。