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_;
97 #endif // !TLX_SEMAPHORE_HEADER size_t value() const
return the current value – should only be used for debugging.
A simple semaphore implementation using C++11 synchronization methods.
size_t value_
value of the semaphore
std::condition_variable cv_
condition variable
Semaphore(Semaphore &&s)
move-constructor: just move the value
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...
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...
size_t signal()
function increments the semaphore and signals any threads that are blocked waiting a change in the se...
std::mutex mutex_
mutex for condition variable
Semaphore & operator=(const Semaphore &)=delete
non-copyable: delete assignment operator
Semaphore(size_t initial_value=0)
construct semaphore
size_t signal(size_t delta)
function increments the semaphore and signals any threads that are blocked waiting a change in the se...