Last active
February 23, 2024 12:48
-
-
Save yohhoy/2156481 to your computer and use it in GitHub Desktop.
binary semaphore for C++11
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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