|
Final Platform Layer 1.0.0
|
This section explains how to create and manage semaphores.
Semaphores are similar to Mutexes but have one major difference: Any Thread can release it!
It internally uses an atomic counter which gets incremented and decremented.
See Synchronization methods for a comparison with the other synchronization primitives.
Call fplSemaphoreInit() to initialize a Semaphore with a fplSemaphoreHandle as an argument. Also, you need to specify the initial value for the Semaphore to start with.
Call fplSemaphoreDestroy() when you are done with that Semaphore to let it release its internal resources.
Call fplSemaphoreWait() to let a Thread wait and decrement the Semaphores value with a fplSemaphoreHandle as an argument.
If the Semaphores value is greater than zero, then the decrement will happen and the function returns immediately.
If the Semaphores value is zero then the Thread will wait until it is possible to decrement the value.
Also, you need to specify the number of milliseconds you want the thread to wait. If FPL_TIMEOUT_INFINITE is passed, it will wait forever.
A thread can only wait on one single Semaphore at a time - if you need to wait on multiple Semaphores, you should consider using Signals .
fplSemaphoreTryWait() is the same as fplSemaphoreWait() , except that if the decrement cannot be performed immediately, the current thread will not be blocked.
Call fplSemaphoreRelease() with the fplSemaphoreHandle in question, to release a Semaphore and signal all waiting Threads.
Call fplSemaphoreValue() to get the current value from the Semaphore.