Using Semaphores

< BACK  NEXT >
[oR]

Semaphores are an integer variable used to count in a synchronized way. Semaphores are often used to control access to a limited resource. For example, if your Windows CE device had two serial communications ports, you might use a semaphore to ensure that an application blocks if all communications ports are in use, and then un-blocks when one is freed. A semaphore object is signaled when less than the maximum number of resources is in use, and non-signaled when all the resources are in use. Semaphores were introduced in Windows CE 3.0. The following steps are used when using a semaphore:

  • The semaphore is created or opened using the CreateSemaphore function (Table 6.6). This function is passed the maximum number of available resources and the initial number of resources in use.

  • A thread calls WaitForSingleObject on the semaphore handle when it needs a resource. The resource count is automatically incremented when WaitForSingleObject returns.

  • A thread calls ReleaseSemaphore when it has finished with the resource. The resource count is decremented.

  • The function CloseHandle is called when the thread has finished with the semaphore.

Table 6.6. CreateSemaphore Creates a new semaphore or opens an existing semaphore
CreateSemaphore
LPSECURITY_ATTRIBUTES lpMutexAttributes Not supported, pass as NULL.
LONG lInitialCount Initial count (usually 0).
LONG lMaximumCount Maximum count, for example, maximum number of available resources.
LPTSTR lpName String containing name of semaphore, or NULL if an unnamed semaphore is being created. If this parameter is NULL a new semaphore is always created.
HANDLE Return Value Handle to new or existing semaphore, or NULL on failure. GetLastError returns ERROR_ALREADY_EXISTS if an existing semaphore was opened.

A thread should call WaitForSingleObject to increment a semaphore's count, and this call will block if the semaphore has reached its maximum count. The function ReleaseSemaphore (Table 6.7) is used to decrement the semaphore's count. As a side effect, this function also returns the semaphore's count before the call to ReleaseSemaphore is made.

Table 6.7. ReleaseSemaphore Decrements a semaphore's count
ReleaseSemaphore
HANDLE hSemaphore Semaphore's handle to decrement
LONG lReleaseCount Pointer to a LONG that contains the previous count before ReleaseSemaphore decremented the count
BOOL Return Value TRUE for success, otherwise FALSE

Interestingly, Windows does not have a function for determining the number of resources in use. The only way to determine this value is to call WaitForSingleObject with a zero timeout. If WaitForSingleObject returns WAIT_TIMEOUT, the semaphore is non-signaled, and the maximum number of resources is in use. For any other return value, ReleaseSemaphore is called, and the number of resources in use is returned in the lReleaseCount variable. Note that the release count is one greater than the actual number of resources in use the call to WaitForSingleObject incremented the count.

Semaphores can be named through the last parameter to CreateSemaphore. This allows a semaphore count to be used by threads in different processes. Windows CE does not support the OpenSemaphore function, but CreateSemaphore can be passed the name of an existing semaphore and it will be opened. In this situation, GetLastError will return ERROR_ALREADY_ EXISTS.


< BACK  NEXT >


Windows CE 3. 0 Application Programming
Windows CE 3.0: Application Programming (Prentice Hall Series on Microsoft Technologies)
ISBN: 0130255920
EAN: 2147483647
Year: 2002
Pages: 181

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net