MFC Grid manual

CLock Class Reference

An instance of CLock class automatically acquires CSyncObject in the constructor and releases it in the destructor. More...

#include <Dapfor/Common/Lock.h>

List of all members.


Public Member Functions

 CLock (CSyncObject &syncObject)
 Constructor.
 ~CLock ()
 Destructor.

Detailed Description

An instance of CLock class automatically acquires CSyncObject in the constructor and releases it in the destructor.

// MyClass.h file

class CMyClass : public Dapfor::Common::CDataObject
{
public:
    double  GetPrice() const;
    void    SetPrice(double newPrice);

private:
    double  m_Price;
    
    mutable Dapfor::Common::CSyncObject m_Sync; //mutable because GetPrice() has const modifier
};



// MyClass.cpp file

double  CMyClass::GetPrice() const
{
    //Protect m_Price value
    Dapfor::Common::CLock lock(m_Sync);
    return m_Price;
}

void  CMyClass::SetPrice(double newPrice)
{
    //The brackets are necessary to send a notification without lock
    {
        //Protect m_Price value
        Dapfor::Common::CLock lock(m_Sync);
        m_Price = newPrice;
    }

    //Send a notification without lock.
    NotifyUpdate(FidPrice);
}

Constructor & Destructor Documentation

CLock ( CSyncObject syncObject  ) 

Constructor.

Waits for an ownership of specified CSyncObject object.

Parameters:
[in] syncObject Synchronization object. Usually a critical section is used.

~CLock (  ) 

Destructor.

Releases all the resources used by unowned CSyncObject object.