sry for the double post, but the edit button vanished Oo
I found a solution, the "setPixel" function isn't atomic, so a mutex fixed it -_-
mutex.lock();
imgDone->setPixel(i, j, qRgb(temp, temp, temp));
mutex.unlock();
mutex.lock();
imgDone->setPixel(i, j, qRgb(temp, temp, temp));
mutex.unlock();
To copy to clipboard, switch view to plain text mode
But now there is another problem: the mutexed setPixel slows the function down massively, even more than single threaded, but it doesn't seem to be because of the mutex. Here a few examples:
// multithreaded, ~11sec runtime on two cores
void Scan::convertToGrayscale() {
[...]
QtConcurrent::run(this, &Scan::convertToGrayscale, i*step, (i+1)*step);
[...]
}
void Scan::convertToGrayscale(int from, int to) {
[...]
mutex.lock();
imgDone->setPixel(i, j, qRgb(temp, temp, temp));
mutex.unlock();
}
}
}
// multithreaded, ~11sec runtime on two cores
void Scan::convertToGrayscale() {
[...]
QtConcurrent::run(this, &Scan::convertToGrayscale, i*step, (i+1)*step);
[...]
}
void Scan::convertToGrayscale(int from, int to) {
[...]
mutex.lock();
imgDone->setPixel(i, j, qRgb(temp, temp, temp));
mutex.unlock();
}
}
}
To copy to clipboard, switch view to plain text mode
// not threaded, ~2sec runtime Oo
void Scan::convertToGrayscale() {
[...]
//QtConcurrent::run(this, &Scan::convertToGrayscale, i*step, (i+1)*step);
convertToGrayscale(i*step, (i+1)*step);
[...]
}
void Scan::convertToGrayscale(int from, int to) {
[...]
mutex.lock();
imgDone->setPixel(i, j, qRgb(temp, temp, temp));
mutex.unlock();
}
}
}
// not threaded, ~2sec runtime Oo
void Scan::convertToGrayscale() {
[...]
//QtConcurrent::run(this, &Scan::convertToGrayscale, i*step, (i+1)*step);
convertToGrayscale(i*step, (i+1)*step);
[...]
}
void Scan::convertToGrayscale(int from, int to) {
[...]
mutex.lock();
imgDone->setPixel(i, j, qRgb(temp, temp, temp));
mutex.unlock();
}
}
}
To copy to clipboard, switch view to plain text mode
OK, was the same code, it seems the mutex is the problem and setPixel seems fast, but lets validate this assumption. According to this assumption the runtime still should be long if I replace the setPixel but keep the mutex:
// multithreaded with mutex but without setPixel, ~2sec
void Scan::convertToGrayscale() {
[...]
QtConcurrent::run(this, &Scan::convertToGrayscale, i*step, (i+1)*step);
[...]
}
void Scan::convertToGrayscale(int from, int to) {
[...]
mutex.lock();
temp = 5;
//imgDone->setPixel(i, j, qRgb(temp, temp, temp));
mutex.unlock();
}
}
}
// multithreaded with mutex but without setPixel, ~2sec
void Scan::convertToGrayscale() {
[...]
QtConcurrent::run(this, &Scan::convertToGrayscale, i*step, (i+1)*step);
[...]
}
void Scan::convertToGrayscale(int from, int to) {
[...]
mutex.lock();
temp = 5;
//imgDone->setPixel(i, j, qRgb(temp, temp, temp));
mutex.unlock();
}
}
}
To copy to clipboard, switch view to plain text mode
The mutex, which seemed to be the problem, doesn't seem to have any impact here, but the setPixel, which seemed to be no problem seems now to be the problem. I don't get it :/
Has anybody experiences with multithreading and imagefiles? I wan't to speed it up because later operations are really time consuming, but if the write process gets slower with multiple threads the whole optimization doesn't make sense. -_-
Bookmarks