OSAPISemaphore Class
The OSAPISemaphore is a counting semaphore for synchronisation. More...
Header: | #include <OSAPI/Process/OSAPISemaphore.h> |
Public Functions
OSAPISemaphore(int initialCount = SEMAPHORE_INITIAL_COUNT) | |
virtual | ~OSAPISemaphore() |
void | Destroy() |
int | Lock() |
void | Release() |
void | Release(int count) |
int | TryLock() |
Detailed Description
The OSAPISemaphore is a counting semaphore for synchronisation.
Default created with inital count == 1 (i.e. signaled, or non-blocked, state). This makes the semaphore behave exactly like a mutex (binary semaphore).
Locking a binary semaphore twice will suspend the execution of the calling thread until some other thread releases the semaphore. Note that locked semaphores do not have to be released by the same thread that locked it. This is not allowed by mutexes.
The semaphore can be used to limit access to resources, but in contrast to mutexes it can allow multiple threads access at the same time. This is done by setting initialCount during construction (e.g. OSAPISemaphore sem(2); allows two threads to lock the semaphore simultaneously).
See also OSAPIMutex and OSAPIMutexLocker.
Member Function Documentation
OSAPISemaphore::OSAPISemaphore(int initialCount = SEMAPHORE_INITIAL_COUNT)
Constructs a semaphore.
[virtual]
OSAPISemaphore::~OSAPISemaphore()
Destructs the semaphore.
void OSAPISemaphore::Destroy()
Destroys the semaphore by releasing all allocated resources.
If semaphore is created, it will be destroyed. This means no thread may be waiting for the semaphore when calling Destroy().
int OSAPISemaphore::Lock()
Locks the semaphore by decreasing lock count.
If already locked the calling process or thread will be suspended until Semaphore is signaled.
Returns 1
if semaphore successfully locked.
void OSAPISemaphore::Release()
Signals and releases the semaphore by increasing lock count by 1.
void OSAPISemaphore::Release(int count)
Signals and releases the semaphore by increasing lock count.
Parameter count | Number of counts to increase. |
int OSAPISemaphore::TryLock()
Tries to lock the semaphore.
Locks if not blocked. Does not wait if blocked. Returns 1
if semaphore successfully locked.
Get started with CDP Studio today
Let us help you take your great ideas and turn them into the products your customer will love.