现在的位置: 首页 > 综合 > 正文

Using Semaphore Objects

2012年08月07日 ⁄ 综合 ⁄ 共 2524字 ⁄ 字号 评论关闭
Using Semaphore Objects

The following example uses a semaphore object to limit the number of threads that can perform a particular task. First, it uses the
CreateSemaphore function to create the semaphore and to specify initial and maximum counts, then it uses the CreateThread function to create the threads.

Before a thread attempts to perform the task, it uses the
WaitForSingleObject
function to determine whether the semaphore's current count permits it
to do so. The wait function's time-out parameter is set to zero, so the
function returns immediately if the semaphore is in the nonsignaled
state. WaitForSingleObject decrements the semaphore's count by one.

When a thread completes the task, it uses the
ReleaseSemaphore function to increment the semaphore's count, thus enabling another waiting thread to perform the task.


Send comments about this topic to Microsoft

Build date: 4/9/2009

抱歉!评论已关闭.