In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how C++ uses the resource handle to automatically manage resources and RAII. I believe most people don't know much about it, so share this article for your reference. I hope you can learn a lot after reading this article. Let's learn about it.
Reason (reason)
To avoid leaks and the complexity of manual resource management. 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.
Avoid leakage and complexity when managing resources manually. The C++ language encourages constructor / destructor symmetry mapping resources to ensure / release essential symmetry contained in function pairs. These function pairs include fopen/fclose,lock/unlock,new/deletre and so on. Whenever you deal with a request / release function that needs to be called in pairs, encapsulate resources with an object that forces pairwise operations-apply for resources in its constructor and release resources in its destructor.
Example, bad (negative example)
Consider (consider the following code):
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.
In this code, you have to remember to unlock,close_port and delete on all paths, and do it exactly once. In addition, if any place is marked. If the code throws an exception, x will leak and my_mutex will remain locked.
Example (sample)
Consider (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.
All resource cleanup is now automatic and is performed once on each path. Whether there is an exception or not. As an additional reward, this function claims to be responsible for ownership of resources.
What is Port? A handy wrapper that encapsulates the resource:
What is Port? A convenient container that encapsulates resources.
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 (Note)
Where a resource is "ill-behaved" in that it isn't represented as a class with a destructor, wrap it ina class or use finally
When a resource has "morbid behavior" because it does not behave as a class with a fictitious function, wrap it in a class or use finally.
These are all the contents of the article "how C++ uses resource handles to automatically manage resources and RAII". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.