0

I have two threads as follows:

Method A

FunctionA()              // Thread 1
{
    mutexA.lock()
    // MapA modification
    mutexA.unlock();
}

FunctionB()               // Thread 2
{
    mutexA.lock()
    // MapA swapped
    mutexA.unlock()
}

Does the above mentioned mutex lock is fast for synchronization or the below logic is fast ? Let me know if the below logic will cause undefined behaviour

Method B

atomic_bool toggleBool = true;
FunctionA()                   // Thread 1
{
  if(GetLockStatus())
  {
  // MapA modified

  toggleBool = true;
  }
}

FunctionB()                 // Thread 2
{
  if(GetLockStatus())
  {
    //MapA Swap
    toggleBool = true;
  }
}

GetLockStatus()               //Common function to get status
{
   // Critical Section or Mutex Lock
   if(toggleBool)
   {
      toggleBool = false;
      return true;
   }
   else
   {
      return false;  
   }
}

Method A or Method B..? Which is faster with less overhead ?

Sel_va
  • 588
  • 5
  • 25

0 Answers0