有哪位能给我讲讲 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 指教。
如果我用 CreateSemaphore(NULL, 0 1, SEMAPHORE_NAME) 的形式创建,则只要有任意一线程调用了 ReleaseSemaphore,则 WaitForSingleObject 也立即返回,如果我想要实现多个线程的同步怎么办呢?比如说我需要确定某 3 个线程同时执行完了之后,WaitForSingleObject 才返回,用 CreateSemaphore 难道就做不到吗?因为 Semaphore 的最小值是零,而一量它的值大于零,则 Semaphore 的装态置为 Signaled 那 CreateSemaphore 里面提供的最小值,最大值还有什么意义呢?请 DX 指教。
解决方案 »
- TrayIcon系统托盘的问题
- socket 问题,请帮忙看看
- 如何做像FoxMail那样的树控件(有单独的数字显示)
- 菜鸟提问!我的程序用来连续显示多幅图片。需要在窗口焦点切换到其它程序后,我的程序仍旧能正常显示连续图片.
- 关于菜单?请高手们指教
- arraylist的用法?
- 有没有WINDOWS MEDIA SERVICE SDK 非在线安装的完全版本?
- 为什么我不能从CRichEditDoc,CRichEditView和CRichEditCntrItem??
- CMenu的问题,如何只用一个id,一个消息函数,响应单击多个item?
- 请问判断系统是不是无盘系统
- 关于CRICHEDITVIEW类的问题
- 请教图像处理问题!
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.
T1 = CreateThread(...);
T2 = CreateThread(...);
T3 = CreateThread(...);..............其它执行过程WaitForSingleObject(T1,INFINITE);
CloseHandle(T1);WaitForSingleObject(T2,INFINITE);
CloseHandle(T2);WaitForSingleObject(T3,INFINITE);
CloseHandle(T3);继续执行....
因为还是菜鸟及别,所以对这些多线程的东东还不是很熟,不好意思了。