Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <future>
- #include <mutex>
- #include <thread>
- class CycleBarrier {
- bool is_outing;
- int entered;
- int N;
- std::mutex m;
- std::condition_variable cv_outing;
- std::condition_variable cv_waiting;
- public:
- CycleBarrier(int N) : N(N),is_outing(false),entered(0) {}
- void attendi(){
- std::unique_lock<std::mutex> l(m);
- while (is_outing) {
- cv_outing.wait(l);
- }
- entered++;
- if (entered == N){
- is_outing=true;
- cv_waiting.notify_all();
- } else {
- cv_waiting.wait(l,[this]{
- return is_outing;
- });
- entered--;
- if (entered==0)
- cv_outing.notify_all();
- }
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement