0

Is it possible to add multiple QFuture's to QFutureWatcher? I wanted to add multiple features to QFutureWatcher and get notified when all the threads finished.

QFutureSynchronizer did not help me as it dont have a signal to finished

ixSci
  • 13,100
  • 5
  • 45
  • 79
NDestiny
  • 1,133
  • 1
  • 12
  • 28
  • QFutureSynchronizer is a template class so it has no signal/slot support. QFutureWatcher works only with one QFuture, so you should track state of every QFuture by yourself. – Jablonski Jun 29 '15 at 06:16
  • @Chernobyl Yes that's what i am doing now, But I am searching is there a straight way to do it. – NDestiny Jun 29 '15 at 06:22
  • Write your own wrapper to keep as much futures as you need. It is easy to write and won't take much time – ixSci Jun 29 '15 at 07:02
  • 1
    @ixSci Yes that's what i am doing now, But I am searching is there a straight way to do it. – NDestiny Jun 29 '15 at 07:05

1 Answers1

0

I'm using it in this way: Where

QFutureSynchronizer<...> _synchronizer;
QFutureWatcher<...> _watcher;

-

void Service::request(const QString& word)
{
    QMutexLocker lock{&_mutex};

    auto future = QtConcurrent::run([this, word]()
                                    {
                                        //...
                                    });
    _synchronizer.addFuture(future);
    if (_watcher.isRunning() == false)
    {
        _watcher.setFuture(future);
    }
}

void Service::onReady()
{
    QMutexLocker lock{&_mutex};
    auto futures = _synchronizer.futures();
    _synchronizer.clearFutures();
    auto removeFrom = std::remove_if(futures.begin()
                                     , futures.end()
                                     , [](const QFuture<QPair<QString, QVector<QString>>>& element)
            {
                return element.isFinished();
            });

    for (auto it = futures.begin(); it != removeFrom; ++it)
    {
        _synchronizer.addFuture(*it);
    }
    for (auto it = removeFrom; it != futures.end(); ++it)
    {
        //...
    }

    auto waitingOnes = _synchronizer.futures();
    if (waitingOnes.isEmpty() == false && _watcher.isFinished())
    {
        _watcher.setFuture(waitingOnes.front());
    }
}
Gelldur
  • 11,187
  • 7
  • 57
  • 68