打开现有信号量时出现错误的句柄

时间:2015-01-31 21:06:13

标签: c++ windows multithreading semaphore

我试图用信号量控制某些进程。因此我写了这堂课:

从ClassSem.hpp中提取

void ClassSem::createSemaphore(char p_semaphoreName[32])
{
#ifdef MODE_WIN
   this->hSemaphore = CreateSemaphore(NULL,0L,2L,p_semaphoreName);
   printf("create Bez. %s - SemID: %i\n",p_semaphoreName,(int)hSemaphore);

   if(this->hSemaphore == NULL)
   {
      this->errnoRet = (uint32_t)GetLastError();
   }
#endif // MODE_WIN
}


void ClassSem::openSemaphore(char p_semaphoreName[32])
{
#ifdef MODE_WIN
   this->hSemaphore = OpenSemaphore(SEMAPHORE_ALL_ACCESS,TRUE,p_semaphoreName);

   printf("open Bez. %s - SemID: %i\n",p_semaphoreName,(int)hSemaphore);

   if(this->hSemaphore == NULL)
   {
      this->errnoRet = (uint32_t)GetLastError();
   }
#endif // MODE_WIN
}


void ClassSem::waitOnSemaphore ()
{
#ifdef MODE_WIN
printf("wait Sem %i - ",(int)this->hSemaphore);
   WaitForSingleObject(this->hSemaphore,LONG_MAX);
#endif // MODE_WIN
}

One Prozess将使用ClObj[n].createSemaphore(semaphoreName[n])创建12个信号量。在另一个过程中,我使用l_obj.createSemaphore(semaphoreName)打开它。 但如果我运行这些过程就会发生这种情况:

主要流程:

create name Global\NSemaphoreProzessID_0000 - SemID: 1788
create name Global\NSemaphoreProzessID_0001 - SemID: 1784
create name Global\NSemaphoreProzessID_0002 - SemID: 1780
create name Global\NSemaphoreProzessID_0003 - SemID: 1776
create name Global\NSemaphoreProzessID_0004 - SemID: 1772
create name Global\NSemaphoreProzessID_0005 - SemID: 1768
create name Global\NSemaphoreProzessID_0006 - SemID: 1764
create name Global\NSemaphoreProzessID_0007 - SemID: 1760
create name Global\NSemaphoreProzessID_0008 - SemID: 1756
create name Global\NSemaphoreProzessID_0009 - SemID: 1752
create name Global\NSemaphoreProzessID_0010 - SemID: 1748
create name Global\NSemaphoreProzessID_0011 - SemID: 1744

另一个过程

open name Global\NSemaphoreProzessID_0010 - SemID: 1788

如果我多次使用l_obj.createSemaphore(semaphoreName),则ID会向上排列。为什么会这样?

非常感谢, 亚历

0 个答案:

没有答案