Skip to content

Instantly share code, notes, and snippets.

@benloong
Created December 20, 2016 08:56
Show Gist options
  • Save benloong/fc02f5498ad80f008acfe5b5e8356335 to your computer and use it in GitHub Desktop.
Save benloong/fc02f5498ad80f008acfe5b5e8356335 to your computer and use it in GitHub Desktop.
C++ semaphore, condition variable mutex based.
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