Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef SEMAPHORE_SEMAPHORE_H
- #define SEMAPHORE_SEMAPHORE_H
- class Semaphore {
- public:
- explicit Semaphore(int threads_cnt);
- void Wait();
- void Notify();
- private:
- int threads;
- std::mutex mut;
- std::condition_variable con_var;
- };
- Semaphore::Semaphore(int threads_cnt) {
- threads = threads_cnt;
- }
- void Semaphore::Wait() {
- std::unique_lock<std::mutex> lock(mut);
- if (threads == 0) {
- con_var.wait(lock);
- threads -= 1;
- }
- }
- void Semaphore::Notify() {
- std::unique_lock<std::mutex> lock(mut);
- if (threads < 0) {
- con_var.notify_one();
- threads++;
- }
- }
- #endif //SEMAPHORE_SEMAPHORE_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement