Ensures that locking is acquired when control enters a scope and released automatically when it leaves.
Also called: Synchronized Lock, Resource-Acquisition-Is_Initialization, Guard, Execute Around Object.
The abstract classclass LOCK { public: virtual void acquire() = 0; virtual void release() = 0; };
The classclass Guard { public: Guard( LOCK & lock ) : lock_( lock ), owner_( false ) { lock_->acquire(); owner_ = true; } ~Guard() { if ( owner_ ) lock_->release(); } private: LOCK * lock_; bool owner_; Guard( const Guard &); // disable copying void opertaor= ( const Guard & ); // disable assignment };
Marco Corvi - Page hosted by geocities.com.{ Guard guard( lock ); // critical section // ... // lock automatically released }