Last active
December 13, 2015 17:58
-
-
Save Hydrotoast/4951369 to your computer and use it in GitHub Desktop.
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
#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