prettify things lab3 partial progress

This commit is contained in:
2026-10-29 21:43:39 +10:00
parent e18440bd58
commit d359357a2b
4 changed files with 75 additions and 66 deletions
+30 -32
View File
@@ -1,37 +1,35 @@
#include <condition_variable>
#include <stdio.h>
#include <mutex>
//Attempt of barrier implementation for case where num_threads*2 -1 >= barrier_count
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;
#include <condition_variable>
#include <mutex>
#include <stdio.h>
class Barrier {
std::mutex mtx;
std::condition_variable cv;
std::size_t count = 0;
std::size_t max_count = 0;
bool flushing = false;
public:
Barrier(size_t max_count) { this->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();
}
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();
}
if (--count == 0) {
flushing = false;
cv.notify_all();
}
}
};
int main(){
}
int main() {}