mardi 4 juin 2019

Reason of using "double if"

I recently read a book about system software. There is an example in this book.

volatile T* pInst = 0;
T* GetInstance()
{
  if (pInst == NULL)
  {
   lock();
   if (pInst == NULL)
     pInst = new T;
   unlock();
  }
  return pInst;
}

Why the author use (pInst == NULL) twice ? Any idea ?

Aucun commentaire:

Enregistrer un commentaire