Using the Guard Mechanism in Multi-Threaded Applications

Using the guard mechanism in multi-threaded applications instead of explicit Lock and Unlock instructions can save you from a number of undesirable synchronization problems.

Developers writing multi-threaded applications often fall into the trap of acquiring a lock—and then failing to release it—which can lead to random blackouts. Even when the lock does get released, it’s sometimes held longer than necessary, because of the need to invoke the locking and unlocking mechanisms explicitly. This type of problem potentially leads to sluggish application performance.

You can eliminate such problems by implementing a guard mechanism. Essentially, the guard is a wrapper over the underlying lock that implicitly locks the scope when the Guard object is constructed, and unlocks the scope when the Guard object gets destroyed.

Here’s an example implementation of a guard that makes use of a CRITICAL_SECTION as the underlying synchronization primitive:

class CLock{public:   virtual int release()=0;   virtual int acquire()=0;   CLock(){};   virtual ~CLock(){};};class CCriticalSectionLock : public CLock{public:   CCriticalSectionLock()   {      InitializeCriticalSection(&m_CriticalSection);   }   virtual ~CCriticalSectionLock()   {      DeleteCriticalSection(&m_CriticalSection);   }   int acquire()   {      EnterCriticalSection(&m_CriticalSection);      return 0;   }   int release()   {      LeaveCriticalSection(&m_CriticalSection);      return -1;   }private:   CRITICAL_SECTION m_CriticalSection;};class CLockGuard{public:   CLockGuard(CLock &lock):m_Lock(lock)   {      acquire();   }   int acquire()   {      m_Owner = m_Lock.acquire();      return m_Owner;   }   int release()   {      if (m_Owner == 0)      {         m_Owner = -1;         m_Lock.release();      }      return -1;   }   ~CLockGuard()   {      release();   }private:   CLock &m_Lock;   int m_Owner; //0 means lock is acquired & -1 means not acquired};

You can use the preceding classes in a program as follows:

//Declare the lock object at some common (global) scopeCCriticalSectionLock g_lock;CThread::MethodRequiringLocking(){   ...   // Code segments that need not be synchronized.   ...   // Code segment that requires synchronization   {      CLockGuard LockGurad(g_lock);      //Perform synchronization sensitive operations   }   ...   // Segments that need not be synchronized.   ...}

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may

man on floor with data

DevX Quick Guide to Data Ingestion

One of the biggest trends of the 21st century is the massive surge in internet usage. With major innovations such as smart technology, social media, and online shopping sites, the internet has become an essential part of everyday life for a large portion of the population. Due to this internet

payment via phone

7 Ways Technology Has Changed Traditional Payments

In today’s digital world, technology has changed how we make payments. From contactless cards to mobile wallets, it’s now easier to pay for goods and services without carrying cash or using a checkbook. This article will look at seven of the most significant ways technology has transformed traditional payment methods.