有哪位能给我讲讲 CreateSemaphore?如果我用 hSem = CreateSemaphore(NULL, 1, 1, SEMAPHORE_NAME) 创建了一个信号量,WaitForSingleObject 不管怎样都是立即返回吗?那这样的创建形式还有什么意义呢?
如果我用 CreateSemaphore(NULL, 0 1, SEMAPHORE_NAME) 的形式创建,则只要有任意一线程调用了 ReleaseSemaphore,则 WaitForSingleObject 也立即返回,如果我想要实现多个线程的同步怎么办呢?比如说我需要确定某 3 个线程同时执行完了之后,WaitForSingleObject 才返回,用 CreateSemaphore 难道就做不到吗?因为 Semaphore 的最小值是零,而一量它的值大于零,则 Semaphore 的装态置为 Signaled 那 CreateSemaphore 里面提供的最小值,最大值还有什么意义呢?请 DX 指教。

解决方案 »

  1.   

    HANDLE OpenSemaphore(
       DWORD fdwAccess, 
       BOOL bInheritHandle, 
       PCTSTR pszName); 
    The lMaximumCount parameter tells the system the maximum number of resources that your application can handle. Since this is a signed, 32-bit value, you can have as many as 2,147,483,647 resources. The lInitialCount parameter indicates how many of these resources are initially (currently) available. When my server process initializes, there are no client requests, so I call CreateSemaphore as follows:HANDLE hsem = CreateSemaphore(NULL, 0, 5, NULL); 
    This creates a semaphore with a maximum resource count of 5, but initially 0 resources are available. (Incidentally, the kernel object's usage count is 1 since I just created this kernel object; don't get the counters confused.) Since the current resource count is initialized to 0, the semaphore is nonsignaled. Any threads that wait on the semaphore are therefore placed in a wait state.A thread gains access to a resource by calling a wait function, passing the handle of the semaphore guarding the resource. Internally, the wait function checks the semaphore's current resource count and if its value is greater than 0 (the semaphore is signaled), the counter is decremented by 1 and the calling thread remains schedulable. The nifty thing about semaphores is that they perform this test-and-set operation atomically; that is, when you request a resource from a semaphore, the operating system checks whether the resource is available and decrements the count of available resources without letting another thread interfere. Only after the resource count has been decremented does the system allow another thread to request access to a resource.If the wait function determines that the semaphore's current resource count is 0 (the semaphore is nonsignaled), the system places the calling thread in a wait state. When another thread increments the semaphore's current resource count, the system remembers the waiting thread (or threads) and allows it to become schedulable (decrementing its current resource count appropriately).A thread increments a semaphore's current resource count by calling ReleaseSemaphore:BOOL ReleaseSemaphore(
       HANDLE hsem, 
       LONG lReleaseCount, 
       PLONG plPreviousCount); 
    This function simply adds the value in lReleaseCount to the semaphore's current resource count. Usually, you pass 1 for the lReleaseCount parameter, but this is certainly not required; I often pass values of 2 or more. The function also returns the current resource count's original value in *plPreviousCount. Few applications actually care about this value, so fortunately you can pass NULL to ignore it.Sometimes it is useful to know the current resource count of a semaphore without actually altering the count, but there is no function that queries a semaphore's current resource count value. At first, I thought that calling ReleaseSemaphore and passing 0 for the lReleaseCount parameter might work by returning the actual count in *plPreviousCount. But this doesn't work; ReleaseSemaphore fills the long variable with 0. Next, I tried passing a really big number as the second parameter, hoping that it would not affect the current resource count because it would take it over the maximum. Again, ReleaseSemaphore filled *plPreviousCount with 0. Unfortunately, there is just no way to get the current resource count of a semaphore without altering it.
      

  2.   

    你把函数用错地方了吧,如果要等线程结束,可以 WaitForMultipleObjectsSemaphore是用来表示可用数量的
      

  3.   

    to test7979(test7979)如果我有如下需求,比如说有很多线程,每个线程在进入的时候将占用资源,信号量加一,在退出的时候释放资源,信号量减一,在我的进程退出的时候,我要确保所有的线程都正常退出,所以要等待这个信号量为零,也就是所有的线程都把资源释放掉了,才能往下处理,该用哪种机制呢?
      

  4.   

    可以在进程退出之前等待线程结束吧,用WaitForxxxObject线程的HANDLE可以用来Wait的
      

  5.   

    比方:
    T1 = CreateThread(...);
    T2 = CreateThread(...);
    T3 = CreateThread(...);..............其它执行过程WaitForSingleObject(T1,INFINITE);
    CloseHandle(T1);WaitForSingleObject(T2,INFINITE);
    CloseHandle(T2);WaitForSingleObject(T3,INFINITE);
    CloseHandle(T3);继续执行....
      

  6.   

    如果仅仅是要对一个东西计数,那么可以用 InterlockedIncrement,InterlockedDecrement使用资源时 InterlockedIncrement,释放时 InterlockedDecrement这样你就知道资源是否都释放了当然也可以用 CriticalSection 来自己计数另外:没有明白"比如由其它进程调用我的线程里面的函数?"
      

  7.   

    呵呵,在写一个 Kernel 下的 Driver 了,我的函数Hook了系统某个函数,如果我的 Driver 卸载时不能确保所有的对我的函数调用都返回了话,则会产生错误,会产生这样的现象,某个进程进入我的 Hook 函数,然后我的函数再调用原来真正的系统调用,当这个系统调用没返回时,我的 Driver 在此时卸载,于是在这个系统函数 return 的时候,将会产生无效页错误。因为 return 要返回的那个地方本应是我的函数的,但是这个函数已经不存在了。所以我要对我的函数的每一次调用记数,卸载时如果还有函数未返回,则不能卸载。
    因为还是菜鸟及别,所以对这些多线程的东东还不是很熟,不好意思了。