Книга: Microsoft Windows Embedded CE 6.0 Exam Preparation Kit
Semaphores
Semaphores
Apart from kernel objects that enable you to provide mutually exclusive access to resources within a process and between processes, Windows Embedded CE also provides semaphore objects that enable concurrent access to a resource by one or multiple threads. These semaphore objects maintain a counter between zero and a maximum value to control the number of threads accessing the resource. The maximum value amount is specified in the CreateSemaphore function call.
The semaphore counter limits the number of threads that can access the synchronization object concurrently. The system will keep decrementing the counter every time a thread completes a wait for the semaphore object until the counter reaches zero and enters the nonsignaled state. The counter cannot decrement past zero. No further thread can gain access to the resource until an owning thread releases the semaphore by calling the ReleaseSemaphore function, which increments the counter by a specified value and again switches the semaphore object back into signaled state.
Similar to mutexes, multiple processes can open handles of the same semaphore object to access resources shared between processes. The first call to the CreateSemaphore function creates the semaphore object with a specified name. You can also construct unnamed semaphores, but these objects are not available for interprocess synchronization. Subsequent calls to the CreateSemaphore function with the same semaphore name do not create new objects, but open a new handle of the same semaphore.
Table 3-13 lists the most important functions that work with semaphore objects for thread synchronization purposes.
Table 3-13 Semaphore API
Function | Description |
---|---|
CreateSemaphore | Creates and initializes a named or unnamed semaphore object with a counter value. Use named semaphore objects to protect resources shared between processes. |
CloseHandle | Closes a semaphore handle and deletes the reference to the semaphore object. All references to the semaphore must be closed individually before the kernel deletes the semaphore object. |
WaitForSingleObject | Waits to be granted ownership of a single semaphore object. |
WaitForMultipleObjects | Waits to be granted ownership for a single or multiple semaphore objects. |
ReleaseSemaphore | Releases a semaphore object. |