I would have liked to comment but I can't because of insufficient reputation. However, note that the top voted answer is suffering from spurious wakeups (see also Can QWaitCondition spuriously wake up?). So a better variant of the answer would be:
#include <QMutex>
#include <QWaitCondition>
#include <QSharedPointer>
// Data "pimpl" class (not to be used directly)
class BarrierData
{
public:
BarrierData(int count) : count(count) {}
void wait() {
mutex.lock();
--count;
if (count == 0) {
condition.wakeAll();
} else {
while(count != 0) {
condition.wait(&mutex);
}
}
mutex.unlock();
}
private:
Q_DISABLE_COPY(BarrierData)
int count;
QMutex mutex;
QWaitCondition condition;
};
class Barrier {
public:
// Create a barrier that will wait for count threads
Barrier(int count) : d(new BarrierData(count)) {}
void wait() {
d->wait();
}
private:
QSharedPointer<BarrierData> d;
};