
Originally Posted by
TorAn
I don't create any blockage in my case. Parent thread starts event loop and need to exit it when all QRunnable processes are done working. I'll appreciate it if you can suggest a better approach.
void func(type1 param1, type2 param2, ...) {
// implement your runnable code here or call some other class/method
}
void Thr::run() {
QFutureSynchronizer<void> sync;
for(int i=0;i<...;++i){
sync.addFuture(QtConcurrent::run(func, param1, param2,...));
}
// QFutureSynchronizer waits for all threads to finish
}
void func(type1 param1, type2 param2, ...) {
// implement your runnable code here or call some other class/method
}
void Thr::run() {
QFutureSynchronizer<void> sync;
for(int i=0;i<...;++i){
sync.addFuture(QtConcurrent::run(func, param1, param2,...));
}
// QFutureSynchronizer waits for all threads to finish
}
To copy to clipboard, switch view to plain text mode
"Thr" doesn't need an event loop. Actually the whole thread is not needed for anything at all, you can have the same logic without it, for instance:
Result func(const Data &data) {
// implement your logic here
}
void MainThread::someFunc() {
QList<Data> data;
data << Data(...);
data << Data(...);
data << Data(...);
data << Data(...);
QFutureWatcher<Result> *watcher = new QFutureWatcher<Result>(this);
connect(watcher, SIGNAL(finished()), SLOT(processingDone()));
QFuture<Result> future = QtConcurrent::mapped(data, func);
watcher->setFuture(future);
}
Result func(const Data &data) {
// implement your logic here
}
void MainThread::someFunc() {
QList<Data> data;
data << Data(...);
data << Data(...);
data << Data(...);
data << Data(...);
QFutureWatcher<Result> *watcher = new QFutureWatcher<Result>(this);
connect(watcher, SIGNAL(finished()), SLOT(processingDone()));
QFuture<Result> future = QtConcurrent::mapped(data, func);
watcher->setFuture(future);
}
To copy to clipboard, switch view to plain text mode
As for your original code -- you can't call parent thread's exit() method from within a different thread. The semaphore thingy also makes no sense, a simple QAtomicInt would do. Better yet, you should exit the event loop from the actual thread when all work is done.
Furthermore from what I see you exit the application immediately after the work is done, so you might as well use blockingMapped() (or even blockingMapReduced() to write results to the file as they come) in my QtConcurrent code and forget about the future watcher.
Bookmarks