用信号量保护数据

 


class CCclRegistrationList
{
 // Public members
public:
 HANDLE              semaphore;
 TCCclRegistration    registrationList;
 
 // Public methods
public:
 CCclRegistrationList();
 ~CCclRegistrationList();
 CCclRegistration* getObject(CCclRegistration* registration);
};

CCclRegistrationList::CCclRegistrationList()
{
 semaphore = CreateSemaphore(NULL, 1, 1000, NULL);  //  创建信号量

}


CCclRegistrationList::~CCclRegistrationList()
{
 TCCclRegistration::iterator  itBegin;
 TCCclRegistration::iterator  itEnd;

 WaitForSingleObject(semaphore, INFINITE);
 itBegin = registrationList.begin();
 itEnd   = registrationList.end();
 for( /*empty*/; itBegin != itEnd; ++itBegin )
 {
  delete *itBegin;
 }
 registrationList.clear();
 ReleaseSemaphore(semaphore, 1, NULL);
 DeleteObject(semaphore);
}

CCclRegistration* CCclRegistrationList::getObject(CCclRegistration* registration)
{
 TCCclRegistration::iterator  itBegin;
 TCCclRegistration::iterator  itEnd;
 CCclRegistration  *        pReg = NULL;

 //modify by msd for debug
 MSD_WaitForSingleObject(semaphore, INFINITE);
 //WaitForSingleObject(semaphore, INFINITE);

 itBegin = registrationList.begin();
 itEnd   = registrationList.end();
 for( /*empty*/; itBegin != itEnd; ++itBegin )
 {
  if(registration == *itBegin)
  {
   pReg = *itBegin;
   break;
  }
 }
 ReleaseSemaphore(semaphore, 1, NULL);
 return pReg;
}

你可能感兴趣的:(用信号量保护数据)