Semaphore is an Erlang library providing functionality similar to that of POSIX semaphores.
To create a semaphore named sem_name
with a value of 2
, use:
semaphore:start_link(sem_name, 2).
This call blocks until the semaphore becomes available. It will always return ok
and is equivalent
to calling semaphore:timed_wait(sem_name, infinity)
.
Example:
semaphore:wait(sem_name).
This is a non-blocking call to the semaphore. It will immediately return either ok
if the semaphore was
acquired, or error
if not.
Eaxmple:
semaphore:try_wait(sem_name).
This call blocks until either the semaphore becomes available or the specified timeout (in milliseconds) is reached.
It returns ok
if the semaphore was acuired, or {error, timeout}
if the timeout was reached.
semaphore:timed_wait(sem_name, 1000).
This call releases the semaphore. It returns ok
if successful and error
if the calling process did not acquire the semaphore.
semaphore:post(sem_name).
This call returns the current value of the semaphore.
semaphore:get_value(sem_name).