SHA256
36 lines
713 B
C++
36 lines
713 B
C++
//Attempt of barrier implementation for case where num_threads*2 -1 >= barrier_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();
|
|
}
|
|
if (--count == 0) {
|
|
flushing = false;
|
|
cv.notify_all();
|
|
}
|
|
}
|
|
};
|
|
|
|
// int main() {}
|