Skip to content

Instantly share code, notes, and snippets.

@Hydrotoast
Last active December 13, 2015 17:58
Show Gist options
  • Save Hydrotoast/4951369 to your computer and use it in GitHub Desktop.
Save Hydrotoast/4951369 to your computer and use it in GitHub Desktop.
#include <thread>
#include <mutex>
class semaphore {
public:
semaphore(unsigned int count) : count_(count) {}
void wait() {
std::lock_guard<std::mutex> lock(mutex_);
while (!count_)
condition_.wait(mutex_);
--count_;
}
void signal() {
std::lock_guard<std::mutex> lock(mutex_);
++count_;
condition_.notify_one();
}
private:
std::mutex mutex_;
std::condition_variable condition_;
unsigned int count_;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment