Three resources management of reading notes of "effective C + +"

Source: Internet
Author: User
Tags mutex

Three resources management of reading notes of "effective C + +"

Preparation of Knowledge:

  1. The so-called resource is that once it is used, it will have to be returned to the system. The most common resources are dynamically allocated memory, and other common resources are file descriptors, mutexes, glyphs and brushes for graphical interfaces, database connections, and network sockets.

  2. Auto_ptr is a "Class pointer object", a so-called smart pointer whose destructor automatically calls delete on the object it refers to. Auto_ptr is located in the #include <memory> header file. Since auto_ptr is automatically deleted when it is destroyed, it is important to be careful not to have multiple auto_ptr pointing to the same object. Auto_ptr has an unusual nature : If you copy them through the copy constructor or the copy assignment operator, they become null, and the copied pointers take the unique ownership of the resource. Auto_ptr is not a powerful weapon to manage the dynamic allocation of resources.

  3. Reference-counting Smart pointer ( reference counting smart pointer RCSP) is an alternative to auto_ptr. Keep track of how many objects point to a resource and are automatically deleted when no one points to it. The behavior provided by Rcsps is similar to garbage collection, but theRcsps cannot break a ring reference (for example, two objects that are not already in use are referred to each other, and thus seem to be in "used" state).

  4. TR1 's tr1::shared_ptr is a RCSP.

  5. Both Auto_ptr and tr1::shared_ptr do delete, not delete[] actions within their destructor . That means that it is unwise to use auto_ptr or tr1::shared_ptr on a dynamically allocated array. But this can be done by compiling.

Prepare knowledge 2auto_ptr Unusual Properties std::auto_ptr<investment> PINV (Createinvestment ());//pinv point to Createinvestment () return std: :auto_ptr<investment> pInv2 (PINV);//Now PInv2 points to the object, PINV is set to NULLPINV = pinv2;//now PINV points to the object, PINV2 is set to null


Body


Article 13: Managing Resources with objects using objects to manage resource

Once the resource is acquired, it is immediately placed within the management object. (The opportunity for resource acquisition is the time of initialization.) Resource acquisition is initialization; short RAII)

Management objects use destructors to ensure that resources are freed.

Examples are as follows:

Class investment{...};investment* createinvestment (); void f ()//auto_ptr version {std::auto_ptr<investment> PINV ( Createinvestment ());//Call the factory function, use PINV to automatically delete Pinv ...} through the destructor of the auto_ptr ...} void f ()//shared_ptr version {...std::tr1::shard_ptr<investment> PINV (Createinvestment ());// PINV points to Createinvestment () return std::tr1::shard_ptr<investment> pInv2 (PINV);//Now Pinv,pinv2 point to the same object PINV = pInv2;// No change ...}

Focus:

    1. To prevent resource leaks, use the Raii object, which obtains resources in the constructor and frees the resources in the destructor.

    2. Two commonly used RAII classes distributed Tr1::shared_ptr and Auto_ptr. The former is usually a better choice, because its copy behavior is more intuitive. If you select Auto_ptr, the copy action causes it to point to null.

2016-11-03 22:23:43


Clause 14: Carefully copying behavior in the resource management class.

When a Raii object is copied, there are several possible

    1. Copying is forbidden.

      Class Lock:private uncopyable{};

    2. The reference counting method is sacrificed to the underlying resource. Sometimes we want to keep resources until the last user of it is destroyed. When you copy a Raii object in this case, you should increment the "referenced number" of the resource. So tr1::shared_ptr. Tr1::shared_ptr allows you to specify the so-called "delete", which is a function or function object that is called when the number of references is 0 o'clock. A second parameter that is optional for the Tr1::shared_ptr constructor.

    3. Copy the bottom resource. Make a deep copy.

    4. Transfer ownership of the bottom resource. Use Auto_ptr.

Class Lock{public://Initializes shared_ptr with a mutex and takes the unlock function as the delete explicit Lock (Mutex *pm): Mutexptr (Pm,unlock) {    Lock (Mutexptr.get ()); }private:std::tr1::shared_ptr<mutex> Mutexptr;}

Focus:

    1. Copying a Raii object must replicate the resources it manages, so the copying behavior of the resource determines the copying behavior of the Raii object.

    2. Common and common Raii class copying behavior is: Suppress copying, execute reference counting method. But other behaviors can also be achieved.

2016-11-03 23:59:30

This article is from the "Do Your best" blog, so be sure to keep this source http://qiaopeng688.blog.51cto.com/3572484/1869161

Three resources management of reading notes of "effective C + +"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.