Go to the documentation of this file.
17 #ifndef TLX_SEMAPHORE_HEADER
18 #define TLX_SEMAPHORE_HEADER
20 #include <condition_variable>
30 explicit Semaphore(
size_t initial_value = 0)
31 :
value_(initial_value) { }
45 std::unique_lock<std::mutex> lock(
mutex_);
52 size_t signal(
size_t delta) {
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_);
70 bool try_acquire(
size_t delta = 1,
size_t slack = 0) {
71 std::unique_lock<std::mutex> lock(
mutex_);
89 std::condition_variable
cv_;
97 #endif // !TLX_SEMAPHORE_HEADER
std::mutex mutex_
mutex for 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...
size_t signal()
function increments the semaphore and signals any threads that are blocked waiting a change in the se...
Semaphore(size_t initial_value=0)
construct semaphore
A simple semaphore implementation using C++11 synchronization methods.
std::condition_variable cv_
condition variable
size_t value() const
return the current value – should only be used for debugging.
size_t value_
value of the semaphore
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 & operator=(const Semaphore &)=delete
non-copyable: delete assignment operator