STRATEGIZED LOCKING

Parametrizes synchronized mechanisms that protect a component's critical sections from concurrent access.


class LOCK
{
  public:
    virtual void acquire() = 0;
    virtual void release() = 0;
};

The implementation of the Guard class in the Scope Locking pattern uses the polymorphism. The following implements the Strategized Locking using parametrized types.


template <class LOCK> class 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.