Posted on 2008-08-27 09:24
没画完的画 阅读(2323)
评论(5) 编辑 收藏 引用 所属分类:
VC
history:
08.08.27 修改部份翻译不当的地方 网友 wangk ; 修改
MSDN中关于信号量的解释如下:
{注:在某些书籍中 Semaphore Objects 翻译为“信标”,有些则翻译为“信号量”,本文均称为“信号量”}
信号量(Semaphore Objects)
A semaphore object is a synchronization object that maintains a count between zero and a specified maximum value. The count is decremented each time a thread completes a wait for the semaphore object and incremented each time a thread releases the semaphore.
信号量是维护0到指定最大值之间的计数器的同步对象.当线程完成一次信号量的等待时,计数器自减1,当线程释放信号量对象时,计数器自增1
{信号量用于对资源进行计数,它包括了
1、带符号的数值 保存最大的资源数
2、带符号的数值 保存当前可用的资源数
}
When the count reaches zero, no more threads can successfully wait for the semaphore object state to become signaled. The state of a semaphore is set to signaled when its count is greater than zero, and nonsignaled when its count is zero.
当计数器为0时,不会再有其它线程等待信号量为受信状态.计数器的值大于0时信号量对象受信,等于0时便不会受信.
The semaphore object is useful in controlling a shared resource that can support a limited number of users.
信号量对象用于支持有限用户的共享资源控制
It acts as a gate that limits the number of threads sharing the resource to a specified maximum number.
它可以作为限制线程共享资源最大值的一种方法
For example, an application might place a limit on the number of windows that it creates.
It uses a semaphore with a maximum count equal to the window limit, decrementing the count whenever a window is created and incrementing it whenever a window is closed. The application specifies the semaphore object in call to one of the wait functions before each window is created. When the count is zero — indicating that the window limit has been reached — the wait function blocks execution of the window-creation code.
例如,一个应用程序限制它所创建的窗体.把信号量的最大值设置为窗体个数的最大值, 当窗体创建时,信号量的值减少,当窗体关闭时,信号量的值增加.应用程序在窗体创建时调用 Wait 系列API函数.当计数为0时,证明窗体个数已经达到极限, wait 系列函数会阻塞创建窗体的代码
A thread uses the CreateSemaphore or CreateSemaphoreEx function to create a semaphore object. The creating thread specifies the initial count and the maximum value of the count for the object. The initial count must be neither less than zero nor greater than the maximum value.
The creating thread can also specify a name for the semaphore object. Threads in other processes can open a handle to an existing semaphore object by specifying its name in a call to the OpenSemaphore function.
线程可以使用 CreateSemaphore 或 CreateSemaphoreEx 创建信号量对象
创建信号量的线程需要指出信号量的初始值和最大值
初始值必须大于0且不大于所指定的最大值
创建的线程同样可以为信号量指定一个名字
其它进程的线程可以通过这个名字,然后调用 OpenSemaphore() 来打开已经存在的信号量对象
For additional information about names for mutex, event, semaphore, and timer objects, see Interprocess Synchronization.
If more than one thread is waiting on a semaphore, a waiting thread is selected. Do not assume a first-in, first-out (FIFO) order. External events such as kernel-mode APCs can change the wait order.
如果多于一条线程在等待信号量对象,wait 返回的那条线程并不会遵循先进先出(FIFO)的顺序,因为内核模式可能改变它的顺序。
Each time one of the wait functions returns because the state of a semaphore was set to signaled, the count of the semaphore is decreased by one. The ReleaseSemaphore function increases a semaphore's count by a specified amount. The count can never be less than zero or greater than the maximum value.
每次信号量受信,wait 系列函数就会返回, 同时计数会减1,
ReleaseSemaphore() 函数用来增加信号量的计数值,这个值永远大于等于0且小于等于指定的最大值
The initial count of a semaphore is typically set to the maximum value. The count is then decremented from that level as the protected resource is consumed. Alternatively, you can create a semaphore with an initial count of zero to block access to the protected resource while the application is
being initialized. After initialization, you can use ReleaseSemaphore to increment the count to the maximum value.
信号量的初始值通常被设置为最大值,当被保护的资源被消耗时,其计数值会被减少。或者,你可以在创建信号值时把初始值指定为 0, 应用程序初始化时,保护资源会阻塞, 初始化后,你可以调用 ReleaseSemaphore 来增加信号量的计数值
A thread that owns a mutex object can wait repeatedly for the same mutex object to become signaled without its execution becoming blocked. A thread that waits repeatedly for the same semaphore object, however, decrements the semaphore's count each time a wait operation is completed; the thread is blocked when the count gets to zero. Similarly, only the thread that owns a mutex can successfully call the ReleaseMutex function, though any thread can use ReleaseSemaphore to increase the count of a semaphore object.
一条线程可以反复地等待 mutex 对象,在它受信前,不会去执行阻塞的代码块
同样,一条线程也可以反复地等待 信号量 对象,
然而,在 wait 操作完成后,信号量的计数会减1,
当为0时,线程会阻塞
同样地,只有当前拥有该mutex 的线程可以调用 ReleaseMutex 函数
但可以在任何线程通过调用 ReleaseSemaphore 函数来增加 信号量的计数
A thread can decrement a semaphore's count more than once by repeatedly specifying the same semaphore object in calls to any of the wait functions. However, calling one of the multiple-object wait functions with an array that contains multiple handles of the same semaphore does not result in multiple decrements.
线程可以通过反复等待一个信号量对象来让信号量的计数自减
但是,如果调用 multiple-object wait 系列函数,等待的数组里指定为多个相同的信号量,这时并不会导致信号量的计数值多次自减。
相关WIN-API函数:
CreateSemaphore()
OpenSemaphore()
ReleaseSemaphore()
WaitForSingleObject()
CloseHandle()
本文内容摘自MSDN,有些地方翻译得不好
欢迎交流,指正
联系QQ: 859934020(经常隐身)
联系E-mail: kredraw@21cn.com