Created
December 20, 2016 08:56
-
-
Save benloong/fc02f5498ad80f008acfe5b5e8356335 to your computer and use it in GitHub Desktop.
C++ semaphore, condition variable mutex based.
This file contains hidden or 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
struct Semaphore | |
{ | |
std::atomic_int count = 0; | |
std::condition_variable cond_var; | |
std::mutex mutex; | |
void wait() { | |
std::unique_lock<std::mutex> lock(mutex); | |
while (!count) | |
{ | |
cond_var.wait(lock); | |
} | |
count--; | |
} | |
void notify() { | |
++count; | |
cond_var.notify_one(); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment