1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
#pragma once
#include <QMutex>
#include <QWaitCondition>
namespace SyntopiaCore {
namespace GLEngine {
class AtomicCounter {
public:
AtomicCounter() : current(0) { };
int increase() {
mutex.lock();
int i = ++current;
mutex.unlock();
wc.wakeAll();
return i;
}
void setValue(int value) {
mutex.lock();
current = value;
mutex.unlock();
wc.wakeAll();
}
bool wait(unsigned long time = ULONG_MAX) { wcm.lock(); bool w = wc.wait(&wcm,time); wcm.unlock(); return w; }
int value() { mutex.lock(); int i = current; mutex.unlock(); return i; }
private:
int current;
QMutex mutex;
QMutex wcm;
QWaitCondition wc;
};
}
}
|