MFC Grid manual

CSyncObject Class Reference

Provides the functionality to protect objects against simultaneous access by two and more threads. CSyncObject internally uses a critical section. More...

#include <Dapfor/Common/SyncObject.h>

List of all members.


Public Member Functions

 CSyncObject ()
 Constructor.
 ~CSyncObject ()
 Destructor.
void Lock ()
 The function waits for ownership of a critical section object.
void UnLock ()
 The function releases ownership of a critical section object.

Detailed Description

Provides the functionality to protect objects against simultaneous access by two and more threads. CSyncObject internally uses a critical section.

// 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

CSyncObject (  ) 

Constructor.

Initializes a critical section object.

~CSyncObject (  ) 

Destructor.

Releases all the resources used by an unowned critical section object.


Member Function Documentation

void Lock (  ) 

The function waits for ownership of a critical section object.

The function returns when a calling thread is granted the ownership.