Can I assign another QFuture object to already running QFuture object? Like in the example below:
QFuture<int> future = QtConcurrent::run(function);
future = QtConcurrent::run(anotherFunciton); //assume that future is still running at this point
I realize that this particular example doesn't make much sense, it's just for the sake of example. Is it safe to make such an assigment, assuming that the result of the first function doesn't interest me anymore? Or do I have to cancel it first? And if so, do I have to wait for cancelation to complete?
QFuture<int> future = QtConcurrent::run(function);
future.cancel();
future.waitForFinished(); //is waiting necessary?
future = QtConcurrent::run(anotherFunciton);
From http://doc.qt.io/qt-4.8/qtconcurrentrun.html:
Note that the QFuture returned by QtConcurrent::run() does not support canceling, pausing, or progress reporting. The QFuture returned can only be used to query for the running/finished status and the return value of the function.
Meaning that even if you wanted to, you couldn't cancel your QFuture. Also, even if cancelling was allowed, why would you cancel it and then wait for it to be finished?
From http://doc.qt.io/qt-4.8/qthreadpool.html#expiryTimeout-prop:
Threads that are unused for a certain amount of time will expire. The default expiry timeout is 30000 milliseconds (30 seconds). This can be changed using setExpiryTimeout(). Setting a negative expiry timeout disables the expiry mechanism.
Since the threads started by QtConcurrent::run()
are managed by the QThreadPool
class, then assuming you didn't specify a negative expiry timeout, you should be able to future = QtConcurrent::run(anotherFunciton);
without issue; the original thread would expire and it would exit. The efficiency of doing this, however, is questionable.