Files
ppsspp/Common/Thread/Barrier.h
T
Henrik RydgårdandClaude Sonnet 5 0f792defb4 CountingBarrier: use notify_all instead of chained notify_one
The notify_one chain (each waiter wakes exactly one more) was correct
but fragile and non-obvious. notify_all is simpler and just as cheap
here since Arrive() is not a hot path.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01L4QAoxV2KY7ek4PcZw3WvY
2026-08-09 19:03:28 +02:00

33 lines
655 B
C++

#pragma once
#include <condition_variable>
#include <mutex>
// Similar to C++20's std::barrier
class CountingBarrier {
public:
CountingBarrier(size_t count) : threadCount_(count) {}
void Arrive() {
std::unique_lock<std::mutex> lk(m);
counter++;
waiting++;
// notify_all (not notify_one) - every waiter needs to see counter >= threadCount_.
cv.wait(lk, [&] {return counter >= threadCount_; });
cv.notify_all();
waiting--;
if (waiting == 0) {
// Reset so it can be re-used.
counter = 0;
}
lk.unlock();
}
private:
std::mutex m;
std::condition_variable cv;
size_t counter = 0;
size_t waiting = 0;
size_t threadCount_;
};