EQuestionAnswers.com Computer/Electronics Questions and Answers
C, C++, VC++, COM/DCOM, DLL and more
#Login #Sign up  Facebook Twitter TGoogle+
 

#Previous question #Index of VC++/WIN32/MFC Questions #Next question

Interlocking APIs

Interlocking is one on the smallest locking mechanism available in Win32. It works on a variable and updates the variable in atomic way. This ensures variable is locked and updated. Synchronization is maintained with respect to multiple thread context. It uses processor instruction set to do an atomic operation.

LONG WINAPI InterlockedIncrement(LPLONG lpAddend);
LONG WINAPI InterlockedDecrement(LPLONG lpAddend);
LONG WINAPI InterlockedExchange(LPLONG Target, LONG Value);
PVOID WINAPI InterlockedCompareExchange(PVOID *Destination,PVOID Exchange,PVOID Comperand);
LONG WINAPI InterlockedExchangeAdd(LPLONG Addend,LONG Value);

Interlocking APIs cannot be used in multiple number of statements. MFC synch objects are used for this purrpose and we are going to discuss more in next section.

CSyncObject class

The base class of all synchonization classes is CSyncObject. CCriticalSection, CEvent, CMutex, CSemaphore etc are derived from this class.

class CSyncObject : public CObject
{
	DECLARE_DYNAMIC(CSyncObject)

// Constructor
public:
	CSyncObject(LPCTSTR pstrName);

// Attributes
public:
	operator HANDLE() const;
	HANDLE  m_hObject;

// Operations
	virtual BOOL Lock(DWORD dwTimeout = INFINITE);
	virtual BOOL Unlock() = 0;
	virtual BOOL Unlock(LONG /* lCount */, LPLONG /* lpPrevCount=NULL */)
		{ return TRUE; }

// Implementation
public:
	virtual ~CSyncObject();

	friend class CSingleLock;
	friend class CMultiLock;
};

Critical section

shared resources can be protected with critical sections. A small block of code can be put under critical section where shared resources are getting accessed.

class CCriticalSection : public CSyncObject
{
	DECLARE_DYNAMIC(CCriticalSection)

// Constructor
public:
	CCriticalSection();

// Attributes
public:
	operator CRITICAL_SECTION*();
	CRITICAL_SECTION m_sect;

// Operations
public:
	BOOL Unlock();
	BOOL Lock();
	BOOL Lock(DWORD dwTimeout);

// Implementation
public:
	virtual ~CCriticalSection();
};

CEvent

Event is mainly used for asynchronous notifaction of a work. Main process creates a thread for doing the actual work and creates an event. Main thread waits on the event to be signaled. Worker thread completes the work and signals the event.

class CEvent : public CSyncObject
{
	DECLARE_DYNAMIC(CEvent)

// Constructor
public:
	CEvent(BOOL bInitiallyOwn = FALSE, BOOL bManualReset = FALSE,
		LPCTSTR lpszNAme = NULL, LPSECURITY_ATTRIBUTES lpsaAttribute = NULL);

// Operations
public:
	BOOL SetEvent();
	BOOL PulseEvent();
	BOOL ResetEvent();
	BOOL Unlock();

// Implementation
public:
	virtual ~CEvent();
};

CEvent is a named object thus can be used accross multiple process.

CMutex

Mutex is the lock for two or more mutual execution. Mutex can have two state locked/owned or unlocked/unowned. One thread can lock a mutex and other threads will wait for unlock. Once the owning thead unlocks the other threads can own and lock.

class CMutex : public CSyncObject
{
	DECLARE_DYNAMIC(CMutex)

// Constructor
public:
	CMutex(BOOL bInitiallyOwn = FALSE, LPCTSTR lpszName = NULL,
		LPSECURITY_ATTRIBUTES lpsaAttribute = NULL);

// Implementation
public:
	virtual ~CMutex();
	BOOL Unlock();
};

CMutex is a named object thus can be used accross multiple process.

CSemaphore

Semaphore is the lock for mutiple state. Multiple states are represented in max count variable passed in constructor. A semaphore with binary state or lock(0) and unlock(1) is same as CMutex.

class CSemaphore : public CSyncObject
{
	DECLARE_DYNAMIC(CSemaphore)

// Constructor
public:
	CSemaphore(LONG lInitialCount = 1, LONG lMaxCount = 1,
		LPCTSTR pstrName=NULL, LPSECURITY_ATTRIBUTES lpsaAttributes = NULL);

// Implementation
public:
	virtual ~CSemaphore();
	virtual BOOL Unlock();
	virtual BOOL Unlock(LONG lCount, LPLONG lprevCount = NULL);
};

CSemaphore is a named object thus can be used accross multiple process.

You have viewed 1 page out of 109. Your VC++ learning is 0.00% complete. Login to check your learning progress.

 Vote 0

Similar topics related to this section

#Programming Windows, Fifth Edition (Developer Reference)
#Programming Applications for Microsoft Windows (Microsoft Programming Series)
#Programming Windows with MFC, Second Edition
#Visual C++ Programming
#Programming Microsoft Visual C++, Fifth Edition

* #1 webmaster Sat 26 Apr/2014 10:21:14(GMT)  Like 0 Unlike 0

Dear Users,

We are pleased to inform you that a forum/blog has been incorporated with www.mybestnotes.co.in. You are welcomed to add your comments, requests, codes, solutions and feedback to it. Please login(if already a member) or signup(for free) to avail to this facility.

Regards,
Webmaster

Your message goes here:

Name:Guest
Email:anonymous@unknown.com
My Post:*
Secutiry Code: ******  *
Preview this compose before posting this in discussion forum.
 
Note:
  1. Use [Sxx] code to show smiles. Example [S02]
  2. Use [URL ], example [URL http://www.google.com/]
  3. To display code or un formatted text use [CODE] [/CODE]. Example: [CODE] printf("Hello world"); [/CODE]
 [S01]   [S02]   [S03]   [S04]   [S05]   [S06]   [S07]   [S08] 
 [S09]   [S10]   [S11]   [S12]   [S13]   [S14]   [S15]   [S16] 
 [S17]   [S18]   [S19]   [S20]   [S21]   [S22]   [S23]   [S24] 

Note: Only members are allowed, * fields are mandatory.