A fast adaptive semaphore lock implementation for cpp!
- Single header file
- MIT License
- Require C++17
All you need to do is to include rezsemaphores.hpp
and you're good to go
#include <rezsem/rezsemaphores.hpp>
You can either grab the latest version or clone the repository and include the include
directory.
Example
$ wget https://raw.githubusercontent.com/scottjr632/resizable-semaphores/v1.0.0/include/rezsem/rezsemaphores.hpp -P rezsem
Semaphores can be initialized
rezsem::Semaphore s(<initial count>, <limit>);
You can acquire a semaphore by using the Acquire
method.
Acquire will block until the requested number of locks are acquired.
rezsem::Semaphore s(<initial count>, <limit>);
s.Acquire(); // will be default increment count by one
s.Acquire(n); // will increment the count by `n`
If you do not want to block waiting on a lock to become free, you can use the
TryAcquire(n)
or TryAcquire()
methods.
rezsem::Semaphore s(<initial count>, <limit>);
s.TryAcquire(); // will not block. Will return true if lock is acquired or false if not
To release the lock you can use
s.Release(); // Releases one by default
s.Release(n); // Releases N
Resizing the limit of the semaphore can be done using
s.SetLimit(n); // s.GetLimit() == n
For examples please see the tests/test_*.cpp files.