Page 323 - DCAP404 _Object Oriented Programming
P. 323

Object-oriented Programming




                    Notes                               CallSomeMethod();
                                                        —SharedVar;
                                                 }
                                   }
                                   When this code is executed as a single thread, CallSomeMethod will execute 20 times. However,
                                   imagine that we start a number of threads, all executing ThreadOne(). However, with more than
                                   one thread existing simultaneously the method CallSomeMethod() will most likely be executed
                                   too many times. Exactly how many times depends on the number of threads spawned, computer
                                   architecture, operating system scheduling and lick. The problem arises because we do not test
                                   and update SharedVar as an atomic operation, so there is a period where the value of SharedVar
                                   is incorrect. During this time other threads can pass the test when they really shouldn’t have.



                                     Did u know?  What is the purpose of SharedVar?
                                     The  value of SharedVar on  exit tells us how  many extra times CallSomeMethod()  is
                                     called. The  value  of  SharedVar on  exit will  be 0  if there  is  a single thread running
                                     CallSomeMethod().
                                   Race conditions can be avoided using an object called mutex.

                                   A mutex is synchronization primitive provided by the  operating system that can be used to
                                   ensure that a section of code executes by one thread at a time. A mutex has two states - locked and
                                   unlocked. In locked state any further attempt to lock it will suspend the thread. The waiting
                                   threads can acquire lock on mutex and resume execution only when the mutex becomes unlocked.
                                   The thread that locks the mutex can only unlocked it. Here is the solution for the above code
                                   using mutex.
                                   int  SharedVar  =  20;
                                   mutex  MutexVar;
                                   void  ThreadTwo()
                                   {

                                                 while(SharedVar  >  0)
                                                 {
                                                        bool  flag  =  false;
                                                        {
                                                        mutex::lock(MutexVar);
                                                        if(SharedVar  >  0)
                                                        {
                                                               —SharedVar;
                                                               flag  =  true;
                                                        }
                                                        }
                                                        if(flag)  CallSomeMethod();
                                                 }
                                   }




          316                               LOVELY PROFESSIONAL UNIVERSITY
   318   319   320   321   322   323   324   325   326   327   328