before practice 3

This commit is contained in:
2026-10-29 11:52:14 +10:00
parent 97fe61de48
commit aa67cb4f88
3 changed files with 49 additions and 5 deletions
+37
View File
@@ -0,0 +1,37 @@
#include <condition_variable>
#include <stdio.h>
#include <mutex>
class Barrier{
std::mutex mtx;
std::condition_variable cv;
std::size_t count=0;
std::size_t max_count=0;
bool flushing = false;
Barrier (size_t max_count){
max_count=max_count;
}
void arrive_and_wait(){
std::unique_lock lock(mtx);
while(flushing)
cv.wait(lock);
if(++count<max_count)
{
while(! flushing)
cv.wait(lock);
}
else{
flushing = true;
cv.notify_all();
}
if(--count==0){
flushing = false;
cv.notify_all();
}
}
};
int main(){
}