Originally Posted by
MarekR22
Anyway can you give this simple example with QtConcurrent?
It could be something like this:
Result parseXml
(const QString &xml
) { ...
}// ...
QFuture<Result> result = QtConcurrent::run(parseXml, xml);
// optionally:
QFutureWatcher<Result> m_watcher;
m_watcher.setFuture(result);
connect(&m_watcher, SIGNAL(finished()), ..., ...);
Result res = result.result(); // or m_watcher.future().result();
Result parseXml(const QString &xml) { ... }
// ...
QString xml = ...;
QFuture<Result> result = QtConcurrent::run(parseXml, xml);
// optionally:
QFutureWatcher<Result> m_watcher;
m_watcher.setFuture(result);
connect(&m_watcher, SIGNAL(finished()), ..., ...);
Result res = result.result(); // or m_watcher.future().result();
To copy to clipboard, switch view to plain text mode
The main advantage over your solution is that if you have a machine with 4 cores, you can run 4 such functions concurrently and with your solution you only have one thread so requests are queued and some CPUs are left idle. Besides I think using QtConcurrent is just much simpler Especially that you don't need the optional part since calling QFuture::result() will block if the result is not ready yet. So you can even have a flow such as this:
QFuture<...> future = QtConcurrent::run(something); // start calculations you'll need later
// more code here to prepare final operation
Result result = future.result(); // may block
doSomethingWith(result, otherThings);
QFuture<...> future = QtConcurrent::run(something); // start calculations you'll need later
// more code here to prepare final operation
Result result = future.result(); // may block
doSomethingWith(result, otherThings);
To copy to clipboard, switch view to plain text mode
Bookmarks