Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.87 KB | None | 0 0
  1. #include <iostream>
  2. #include <future>
  3. #include <mutex>
  4. #include <thread>
  5.  
  6. class CycleBarrier {
  7.     bool is_outing;
  8.     int entered;
  9.     int N;
  10.     std::mutex m;
  11.     std::condition_variable cv_outing;
  12.     std::condition_variable cv_waiting;
  13.    
  14. public:
  15.     CycleBarrier(int N) : N(N),is_outing(false),entered(0) {}
  16.    
  17.     void attendi(){
  18.         std::unique_lock<std::mutex> l(m);
  19.        
  20.         while (is_outing) {
  21.             cv_outing.wait(l);
  22.         }
  23.        
  24.         entered++;
  25.        
  26.         if (entered == N){
  27.             is_outing=true;
  28.             cv_waiting.notify_all();
  29.         } else {
  30.             cv_waiting.wait(l,[this]{
  31.                 return is_outing;
  32.             });
  33.            
  34.             entered--;
  35.            
  36.             if (entered==0)
  37.                 cv_outing.notify_all();
  38.         }
  39.        
  40.     }
  41.    
  42.    
  43. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement