Objective C ++ Study Notes-Clause 14, objective

Source: Internet
Author: User

Objective C ++ Study Notes-Clause 14, objective

* *********************************** Please specify the source: bytes ********************************************




Iii. Resource Management

 

 

Rule 14: Think carefully about copying behavior in resource-managing classes.

Rule 14: copying behavior in resource management


1. Introduction

In clause 13, the following concept is introduced: "The time when resources are obtained is the initialization time" (RAII), which serves as the spine of "Resource Management, it also describes how auto_ptr and tr1: shared_ptr express this concept on heap-based resources. △ However, not all resources are heap-based, so you may need to establish your own resource management class. △.

 


2. Create your own resource management class

Assume that we use the c api function to process Mutex mutex objects (Mutex objects). There are two functions: lock and unlock:

<Span style = "font-family: Comic Sans MS;"> void lock (Mutex * pm); // lock the Mutex void unlock (Mutex * pm ); // unlock the mutex </span>


To ensure that we do not forget to unlock a locked Mutex, we may want to create a class to manage it and release it during the analysis:

<span style="font-family:Comic Sans MS;">class Lock  {public:    explicit Lock(Mutex* pm) : mutexPtr(pm)    {  lock(mutexPtr);  }    ~Lock()  {  unlock(mutexPtr);  }private:    Mutex *mutexPtr;};</span>



The customer's usage of Lock will also comply with the RAII method:

<Span style = "font-family: Comic Sans MS;"> Mutex m; // defines the Mutex needed... {// create a block to define Lock m1 (& m) in the critical section; // Lock the mutex ...} // automatically unlock the mutex at the end of the block </span>


This is good, but what if the Lock Object is copied?

This is a specific example of a generalization problem, but this generalization problem is something that every RAII author must face.

In most cases, you may choose the following two possibilities:


☆Prohibit Replication

In many cases, it is not reasonable to allow the RAII object to be copied. Disable it. In Clause 6, we will show you how to: declare the copying operation as private. In this example:

<Span style = "font-family: Comic Sans MS;"> class Lock: private Uncopyable {public:... // same as before}; </span>


☆Offering reference-count to underlying resources)

Sometimes we want to retain resources until the last user (an object) of the resource is destroyed. In this case, when copying a RAII object, you should increase the number of referenced resources progressively. Tr1: shared_ptr.

Generally, RAII can implement the reference-counting copying action as long as it contains a tr1: shared_ptr member variable. If the aforementioned Lock intends to use reference couting, it can change the mutexPtr type and change it from Mutex * To tr1: shared_ptr <Mutex>. Unfortunately, however, tr1: the default behavior of shared_ptr is "Delete the specified object when the number of references is 0", which is not what we want. But when we use Mutex, the desired release action is unlock rather than Delete.

Fortunately, tr1: shared_ptr allows you to specify the so-called "Er", which is a function or function object, it is called when the number of references is 0 (this function does not exist in auto_ptr ). The delimiters are optional for tr1: shared_ptr, so the code is as follows:

<Span style = "font-family: Comic Sans MS;"> class Lock {public: explicit Lock (Mutex * pm): mutexPtr (pm, unlock) {lock (mutexPtr. get ();} private: std: tr1: shared_ptr <Mutex> mutexPtr; // replace raw pointer with shared_ptr. </span>

In this example, the Lock class does not declare the destructor. Because it is unnecessary.


△Copy the bottom Resource

Sometimes, as long as you like, you can have any number of copies for a resource ). The only reason you need "Resource Management" is that when you no longer need a replica, make sure it is released. In this case, when copying a resource management object, you should also copy its resources, that is, when copying an object, perform "Deep copy ".

Some standard string types are composed of pointers pointing to heap memory. This string object contains a pointer pointing to a heap memory. When such a String object is copied, no matter the pointer or memory it refers to, a replica will be produced. Such a string shows deep copying ).


△ Transfer bottom resource ownership

In some cases, you may want to ensure that there is always only one RAII object pointing to an unprocessed resource, even if the RAII object is copied. At this time, the resource ownership will be transferred from the Copied object to the target object. As stated in the 13th clause, this is the replication significance pursued by auto_ptr.


The Copying function may be automatically created by the compiler. Therefore, unless the version generated by the compiler does what you want, you need to write it yourself.



3. Remember:

★To copy a RAII object, you must copy the resources it manages. Therefore, the copying behavior of the resource determines the copying behavior of the RAII object.

★The common and common RAII copying behaviors are copying suppression and reference counting ). However, other actions may also be implemented.

 



* *********************************** Please specify the source: bytes ********************************************

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.