Class Secure

Class Secure

Placement in the overall structure

Module
guard
Base class
None
Derived classes
None
Time of creation
Task 3
Files
secure.h

Description

The Secure class is used for convenient protection of critical sections. It exploits the fact that the C++ compiler automatically includes constructor and destructor calls in the code for each object and that an object loses its validity as soon as it leaves the scope in which it was declared.

Therefore, if a critical section is entered in the constructor of Secure and exited in the destructor, marking critical code sections can be done quite simply as follows:

    // uncritical
    ...
     { Secure section;
       // here come the critical instructions 
       ...
     }
    // end of the critical section

Public methods

Secure ()
In the constructor, the critical section protected by the Guard object guard is entered.
~Secure ()
In the destructor, the critical section is exited.

Notes

The methods of the class are so short that it is best to inline them.