void ThreadWatcher::run() {
for (int i = 0; i < 6; i++) {
connect(thread[i], SIGNAL(finished()), &threadMapper, SLOT(map()));
threadMapper.setMapping(thread[i], i); }
connect(&threadMapper, SIGNAL(mapped(int)), this, SLOT(finishedHandler(int)));
connect(updateTimer, SIGNAL(timeout()), this, SLOT(timeoutHandler()));
connect(this, SIGNAL(refresh(int)), window, SLOT(draw(int)));
connect(this,
SIGNAL(time(int,
QString)), window,
SLOT(setTimeLabel
(int,
QString)));
connect(this,
SIGNAL(ratio
(int,
QString)), window,
SLOT(setRatioLabel
(int,
QString)));
updateTimer->start(timerVal);
delete updateTimer;
}
void ThreadWatcher::timeoutHandler() {
for(int i=0; i<6; i++) {
if (alive[i])
emit refresh(i); }
if ((threadsLeft==0) || (control[1])) {
else if (control[0]) {
updateTimer->stop();
lock->lock();
condition->wait(lock);
lock->unlock();
updateTimer->start(timerVal); }
}
void ThreadWatcher::finishedHandler(int t) {
emit refresh(t);
alive[t] = false;
threadsLeft--;
seconds[t] = (float)runTime[t] / 1000.0f;
if (seconds[t] == 0.0f)
seconds[t] = 0.001f;
emit
time(t,
"Time: "+QString::number(seconds
[t
],
'g',
3));
if (isFirst) {
isFirst = false;
min = t;
emit ratio(t, "Ratio: 1x"); }
else {
float timeRatio = seconds[t]/seconds[min];
if (timeRatio < 1.0f)
fixRatios(t);
else
emit ratio
(t,
"Ratio: "+QString::number(timeRatio,
'g',
3)+"x");
}}
inline void ThreadWatcher::fixRatios(int t) {
long minTime = runTime[min];
long x;
for (int i=0; i<6; i++) {
if (!alive[i]) {
x = runTime[i];
if ((x > -1) && (x < minTime)) {
minTime = x;
min = i; } } }
for (int j=0; j<6; j++) {
if (!alive[j]) {
if (j==min)
emit ratio(j, "Ratio: 1x");
else
emit ratio
(j,
"Ratio: "+QString::number(seconds
[j
]/seconds
[min
],
'g',
3)+"x");
} }}
void ThreadWatcher::run() {
QSignalMapper threadMapper;
updateTimer = new QTimer();
for (int i = 0; i < 6; i++) {
connect(thread[i], SIGNAL(finished()), &threadMapper, SLOT(map()));
threadMapper.setMapping(thread[i], i); }
connect(&threadMapper, SIGNAL(mapped(int)), this, SLOT(finishedHandler(int)));
connect(updateTimer, SIGNAL(timeout()), this, SLOT(timeoutHandler()));
connect(this, SIGNAL(refresh(int)), window, SLOT(draw(int)));
connect(this, SIGNAL(time(int, QString)), window, SLOT(setTimeLabel(int, QString)));
connect(this, SIGNAL(ratio(int, QString)), window, SLOT(setRatioLabel(int, QString)));
updateTimer->start(timerVal);
QThread::exec();
delete updateTimer;
}
void ThreadWatcher::timeoutHandler() {
for(int i=0; i<6; i++) {
if (alive[i])
emit refresh(i); }
if ((threadsLeft==0) || (control[1])) {
QThread::exit(0); }
else if (control[0]) {
updateTimer->stop();
lock->lock();
condition->wait(lock);
lock->unlock();
updateTimer->start(timerVal); }
}
void ThreadWatcher::finishedHandler(int t) {
emit refresh(t);
alive[t] = false;
threadsLeft--;
seconds[t] = (float)runTime[t] / 1000.0f;
if (seconds[t] == 0.0f)
seconds[t] = 0.001f;
emit time(t, "Time: "+QString::number(seconds[t],'g',3));
if (isFirst) {
isFirst = false;
min = t;
emit ratio(t, "Ratio: 1x"); }
else {
float timeRatio = seconds[t]/seconds[min];
if (timeRatio < 1.0f)
fixRatios(t);
else
emit ratio(t, "Ratio: "+QString::number(timeRatio,'g',3)+"x"); }
}
inline void ThreadWatcher::fixRatios(int t) {
long minTime = runTime[min];
long x;
for (int i=0; i<6; i++) {
if (!alive[i]) {
x = runTime[i];
if ((x > -1) && (x < minTime)) {
minTime = x;
min = i; } } }
for (int j=0; j<6; j++) {
if (!alive[j]) {
if (j==min)
emit ratio(j, "Ratio: 1x");
else
emit ratio(j, "Ratio: "+QString::number(seconds[j]/seconds[min],'g',3)+"x"); } }
}
To copy to clipboard, switch view to plain text mode
Bookmarks