RaiI of C ++

Source: Internet
Author: User

-- A lot of things are summarized after reading books and learning from the Internet:

Simple and practical things have been used many times, and I don't know whether it was raiI --- resource acquire is initial.

Originated from the idea of Bjarne stroustrup, founder of C ++, it mainly solves Resource Management (mainly release issues). Compared with Java, C ++ releases resources, more needs to be manually processed by programmers.

Therefore, you can use the object life cycle (the object on the stack) to manage resources at the beginning and end of the object Life Cycle (any resource can be a connection or a piece of memory ), object creation (initialization) and object extinction (release resources );

Most of the time, I also get used to calling the stack object proxy, which is just an intermediary for managing specific resources. In the following example, an autolock instance is only a proxy for the system resource in the critical section, the resource to be managed is a critical zone.


Class autolock
{
Public:
Autolock ()
{
Init *** (mcriticalsection) // initialize the critical section
}
~ Autolock ()
{
Delete ** (mcriticalsection) // deletes the critical section.
}

Void lock ()
{
Enter ** () // enter the critical section
}

Void unlock ()
{
Leave *** () // introduces the critical section
}

Critical_section mcriticalsection;
};


If an object autolock mylock is within its lifecycle, it is responsible for managing and eliminating the critical zone resources of the cause.

To some extent, it solves some resource management troubles.

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.