17 #ifndef TLX_SEMAPHORE_HEADER
18 #define TLX_SEMAPHORE_HEADER
20 #include <condition_variable>
31 :
value_(initial_value) { }
45 std::unique_lock<std::mutex> lock(
mutex_);
53 std::unique_lock<std::mutex> lock(
mutex_);
54 size_t res = (
value_ += delta);
60 size_t wait(
size_t delta = 1,
size_t slack = 0) {
61 std::unique_lock<std::mutex> lock(
mutex_);
62 while (
value_ < delta + slack)
71 std::unique_lock<std::mutex> lock(
mutex_);
72 if (
value_ < delta + slack)
89 std::condition_variable
cv_;
A simple semaphore implementation using C++11 synchronization methods.
std::mutex mutex_
mutex for condition variable
Semaphore(size_t initial_value=0)
construct semaphore
size_t value() const
return the current value – should only be used for debugging.
size_t signal()
function increments the semaphore and signals any threads that are blocked waiting a change in the se...
std::condition_variable cv_
condition variable
size_t wait(size_t delta=1, size_t slack=0)
function decrements the semaphore by delta and blocks if the semaphore is < (delta + slack) until ano...
Semaphore & operator=(const Semaphore &)=delete
non-copyable: delete assignment operator
size_t signal(size_t delta)
function increments the semaphore and signals any threads that are blocked waiting a change in the se...
bool try_acquire(size_t delta=1, size_t slack=0)
function decrements the semaphore by delta if (delta + slack) tokens are available as a batch.
Semaphore(const Semaphore &)=delete
non-copyable: delete copy-constructor
size_t value_
value of the semaphore
Semaphore(Semaphore &&s)
move-constructor: just move the value