Sunday, March 25, 2007

CMutex with CSingleLock application

1.CSingleLock with CMutex.... MFC...

One thread incremented the counter , another one thread displays the value in the dialog box....

I have to synchronize the things...


Description :

1. CMutex object is available as a member in dialog application...

2. we developed the CCounterThread(increment the value) and CDisplayThread

used to update the value to the dialog box...

CCounterThread and CDisplayThread classes are derived from CWinThread...


The Dialog box have the string value for the counter (displayed in Text Box)...

within the counter thread, we incremented the counter and displayed the value in DisplayThread...

class CCounterThread : public CWinThread
{
public:
BOOL m_bDone;
CCounterThread() { m_bDone = FALSE; }
virtual BOOL InitInstance () { return TRUE; }
virtual int Run();
CMutexDlg* dlg; //Set this dialog at runtime...
};

int CCounterThread ::Run ()
{
int i =0;
CSingleLock sLock(&dlg->m_MutexObject);

while(!m_bDone)
{

sLock.Lock();
dlg->m_Number = i; // m_Number is mapped to the dialog's Edit box...
sLock.UnLock();

}

SendMessage(dlg,WM_CLOSE,0,0);

}
CMutexDlg has CCounterThread and CDisplayThread object, CMutex object...
CCounterThread and CDisplayThread must have the Dialog (CMutexDlg) object..


I derived the CWinThread class...

The CWinThread derived class has overriding Run() fn...

No comments: