PDA

View Full Version : QtConcurrent question



bair
21st March 2009, 22:19
I am writing a program which performs some heavy calculations, so I'm exploring QtConcurrent. I've looked at the QtConcurrent Progress Dialog Example (http://doc.trolltech.com/4.5/qtconcurrent-progressdialog-main-cpp.html) and tried to use QtConcurrent::map() in the same way. Here is my code:

void MainWindow::some_member_function()
{
...
QVector<int> vector;
int iterations=int(Ntotal/1000000)+1;
for (int i = 0; i < iterations; ++i)
vector.append(i);
...
futureWatcher.setFuture(QtConcurrent::map(vector, cycle)); // <----- error
...
}
void MainWindow::cycle(int &start)
{
//lots of calculations
...
}
I'm getting this error:

mainwindow.cpp:345: error: argument of type `void (MainWindow::)(int&)' does not match `void (MainWindow::*)(int&)'
I'm confused. How should I use my member function cycle() in QtConcurrent::map() properly?

bair
22nd March 2009, 07:58
Ok, now I see it was a stupid C++ question, sorry. Solved the problem by making cycle and member variables it uses static. Now everything seems to work, I've got 4x speedup.

bair
22nd March 2009, 15:11
Things turn out to be not so easy. Yes, I've got 4x speedup, but cycle() function generates huge amount of data (hundreds of megabytes) which should be written to a file, but my attempts to write output data from that function have no success. In my MainWindow class I have static QDataStream member out, and I'm using it for output. During execution there is a lot of qDebug messages "QIODevice::write: ReadOnly device", and output file remains absolutely empty, though if I write something to out before QtConcurrent::map() is started, it is written to a file without problems.
As far as I understand, multiple threads generated by QtConcurrent::map() somehow automatically lock out and prevent each other from writing to it. Or may be I am wrong? Anyway, what could be done to avoid this?

Please give me a hint, I'm stuck and this thing is starting to drive me crazy...

bair
24th March 2009, 06:03
Any suggestion would be appreciated. Even something like "RTFM".

wysota
24th March 2009, 07:39
You can't access a QIODevice from multiple threads at once, that's for sure. Try returning the data from calculations to the main thread and then writing it sequentially from there.

bair
24th March 2009, 10:46
Thank you very much. I was thinking about returning data from the calculations to the main thread, but the problem is that there's too much data to handle, so I was afraid that memory consumption will be unacceptably high. Now I think I'll manage it.

Thanks again, you saved me from many hours of attempts to walk through a brick wall.