ref count is a very useful tech in managing resource in c/c++
 
 
 
simply in c we can use the following code to implement ref count
 
 
 
UPSINFO *attach_ups(UPSINFO *ups)
{
   if (!ups)
      return new_ups();
   P(ups->mutex);
   ups->refcnt++;
   V(ups->mutex);
   return ups;
}
void detach_ups(UPSINFO *ups)
{
   P(ups->mutex);
   ups->refcnt--;
   if (ups->refcnt == 0) {
      destroy_ups(ups);
   }
}
void destroy_ups(UPSINFO *ups)
{
   if (ups->refcnt == 0) {
      free(ups);
   }
   pthread_mutex_destroy(&ups->mutex);
}