In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to use shared_ptr sharing ownership in C++". In daily operation, I believe many people have doubts about how to use shared_ptr sharing ownership in C++. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts about "how to use shared_ptr sharing ownership in C++". Next, please follow the editor to study!
F.27 use shared_ptr to share ownership
Reason (reason)
Using std::shared_ptr is the standard way to represent shared ownership. That is, the last owner deletes the object.
Using std::shared_ptr is the standard way to express ownership sharing. In this way, the last owner is responsible for destroying the object.
Example (sample)
Shared_ptr im {read_image (somewhere)}
Std::thread t0 {shade, args0, top_left, im}; std::thread T1 {shade, args1, top_right, im}; std::thread T2 {shade, args2, bottom_left, im}; std::thread T3 {shade, args3, bottom_right, im}
/ / detach threads// last thread to finish deletes the image
Translator's note: the code first constructs an Image object and manages it using shared_ptr. Then pass them to four threads to share the object. This code starts four threads and then exits the scope of the im. When all threads end, the last ending thread is responsible for destroying the Image object.
Note (Note)
Prefer a unique_ptr over a shared_ptr if there is never more than one owner at a time.shared_ptr is for shared ownership.
Note that pervasive use of shared_ptr has a cost (atomic operations on the shared_ptr's reference count have a measurable aggregate cost).
Unique_ptr is more appropriate than shared_ptr if there will never be more than one owner at a time.
Alternative (optional)
Have a single object own the shared object (e.g. A scoped object) and destroy that (preferably implicitly) when all users have completed.
Let an object manage a shared object (such as a scope object) and destroy it (preferably implicitly) when all users end up using it.
Enforcement (implementation recommendations)
(Not enforceable) This is a too complex pattern to reliably detect.
The (non-executable) pattern is too complex to check reliably.
At this point, the study on "how to use shared_ptr to share ownership in C++" is over. I hope I can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.