Share via


Semaphore Objects (Windows CE 5.0)

Send Feedback

A semaphore is an interprocess synchronization object that maintains a count between zero and a maximum value.

The object's state is set to signaled when its count is greater than zero, and nonsignaled when its count is zero.

A semaphore is used as a counting resource gate, limiting the use of a resource by counting threads as they pass in and out of the gate.

Each time a waiting thread is released because of the signaled state of a semaphore, the count of a semaphore is decreased by one.

Use the CreateSemaphore function to create a named or unnamed semaphore object.

The handle returned by CreateSemaphore can be used in any function that requires a handle to the semaphore object.

Any thread of a calling process can specify the semaphore-object handle in a call to a wait function.

Use the ReleaseSemaphore function to increment a semaphore count by a specified amount. The count can never be less than zero or greater than the initial count specified by the CreateSemaphorelInitialCount parameter.

Typically, an application uses a semaphore to limit the number of threads using a resource.

Before a thread uses the resource, it specifies the semaphore handle in a call to a wait function. When the wait function returns, it decreases the semaphore count by one and the waiting thread is released to continue its execution.

When a thread finishes using a resource, the thread calls ReleaseSemaphore to increase the semaphore count by one.

ReleaseSemaphore can also be used during initialization of an application.

The application can create a semaphore with an initial count of zero. This sets the semaphore state to nonsignaled and blocks all threads from accessing the protected resource.

When the application finishes initialization, it uses ReleaseSemaphore to increase the count to its maximum value and permit normal access to the protected resource.

Semaphores can be used across processes or within a single process.

Multiple processes can have handles of the same semaphore object. This provides synchronization between objects. The CreateSemaphore function is always used for this, because Windows CE does not implement the OpenSemaphore function.

Use the CloseHandle function to close the handle. The system closes the handle when the process terminates. When you close the last handle for a semaphore object, the semaphore object is destroyed.

See Also

Synchronization

Send Feedback on this topic to the authors

Feedback FAQs

© 2006 Microsoft Corporation. All rights reserved.