Skip to content

Instantly share code, notes, and snippets.

@yohhoy
Last active February 23, 2024 12:48
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yohhoy/2156481 to your computer and use it in GitHub Desktop.
Save yohhoy/2156481 to your computer and use it in GitHub Desktop.
binary semaphore for C++11
#include <mutex>
#include <condition_variable>
// binary semaphore
class binsem {
public:
explicit binsem(int init_count = count_max)
: count_(init_count) {}
// P-operation / acquire
void wait()
{
std::unique_lock<std::mutex> lk(m_);
cv_.wait(lk, [=]{ return 0 < count_; });
--count_;
}
bool try_wait()
{
std::lock_guard<std::mutex> lk(m_);
if (0 < count_) {
--count_;
return true;
} else {
return false;
}
}
// V-operation / release
void signal()
{
std::lock_guard<std::mutex> lk(m_);
if (count_ < count_max) {
++count_;
cv_.notify_one();
}
}
// Lockable requirements
void lock() { wait(); }
bool try_lock() { return try_wait(); }
void unlock() { signal(); }
private:
static const int count_max = 1;
int count_;
std::mutex m_;
std::condition_variable cv_;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment