0

I am running 5 threads as below

for(int i=0; i< 5 ; i++
{
   QtConcurrent::run()
}

I want to wait here till all the threads get finished.

void QFutureSynchronizer::waitForFinished ()  is hanging my main GUI.

So I want to use some thing like this QEventLoop::exec();

But how to exit out of this exec() once all the results available?

Edited: - I did some thing like this, that works for me

constructor()
{
   m_noOfThreadsFinished = 0;
   m_totalThreads = 5;

    for(int i=0; i< 5 ; i++
    {
       QFuture<void> l_future =  QtConcurrent::run();
       QPointer< QFutureWatcher<void> >  l_futurewatcher = new  QFutureWatcher<void>();
        connect(l_futurewatcher, SIGNAL(finished()), this, SLOT(FinishedThread()) );
        l_futurewatcher->setFuture(l_future);    
    }

    if(eventLoop != NUL)
          delete eventLoop;
    eventLoop = new QEventLoop(); // QPointer<QEventLoop> eventLoop; is class member

     //start event loop here, so that GUI wont block
     eventLoop->ecec(); 

    //do things after all threads finished
}

void FinishedThread() //slot 
{
    QFutureWatcher<void>* l_futurewatcher = static_cast< QFutureWatcher<void>* > (sender());
    l_futurewatcher->deleteLater();

   if( (++m_noOfThreadsFinished == m_totalThreads) && !m_processCancelled)
   {
        emit finishedreading();
   }
}

void FinishedAllThreads() //slot for finishedreading 
{
    killLocalEventLoop();
}

void killLocalEventLoop()
{
  //QPointer automatically make eventLoop to NULL, when it got deleted 
  eventLoop->quit();
  eventLoop->deleteLater();
}

NOTE : you people can ask why cann't I do the things in FinishedThread() slot when I get to know all threads are finished, project I am working in forcing me to do the things in constructor only when all threads finished(This may not be situation for you then you can do things in FinishedThread() slot).

Cœur
  • 37,241
  • 25
  • 195
  • 267
NDestiny
  • 1,133
  • 1
  • 12
  • 28
  • How about start creating and wainting threads finished at separate thread with own(non-GUI) event loop? – t3ft3l--i Jun 30 '15 at 09:41
  • I dont want to use low level threads here, I just looking for how can I come out of exec() once I done with all the threads ?? – NDestiny Jun 30 '15 at 09:46
  • 1
    How about using signal & slots? – Sommerwild Jun 30 '15 at 09:51
  • Why can't you use `QFutureWatcher`? – m.s. Jun 30 '15 at 10:41
  • I found that QFutureWatcher hangs my main gui - at least if it is inside a button click handler. (The title bar had "(Not responding" on it). I used a progress dialog to hide this, but that isn't appropriate in all cases. – Michael Vincent Jun 30 '15 at 11:04
  • I can not use QFutureWatcher @m.s. big story to write . – NDestiny Jun 30 '15 at 13:26
  • @MichaelVincent I am having a same problem – NDestiny Jun 30 '15 at 13:27
  • @Durga do you have a [MCVE](http://stackoverflow.com/help/mcve) which reproduces the issue you have with `QFutureWatcher`? – m.s. Jun 30 '15 at 13:28
  • @m.s. if you remove the progress dialog from the tutorial here: http://www.bogotobogo.com/Qt/Qt5_QtConcurrent_QFutureWatcher_QProgressDialog_map.php you will see the problem with the application not responding when using QFutureWatcher inside a button click. – Michael Vincent Jun 30 '15 at 14:29
  • @Durga I haven't treid it, but I wonder if one solution would be to move the calling code into another non-gui thread, then call the 5 threads from that thread. A single signal back to the gui from the intermediate thread could be sent when all other threads are complete. – Michael Vincent Jun 30 '15 at 14:33
  • @MichaelVincent why would one use `waitForFinished`? of course this will block the GUI. `QFutureWatcher` provides a `finished`signal, use this! – m.s. Jun 30 '15 at 14:35
  • @m.s. Ah - ok. I'll give it a go. Thanks. – Michael Vincent Jun 30 '15 at 15:10
  • @Durga thanks for the update – Michael Vincent Jul 02 '15 at 08:38

1 Answers1

2

The solution is quite simple: don't ever use any waitForXxx methods in Qt. They are never necessary.

Instead, you should be connecting to the QFutureWatcher::finished() signal. See, for example, this answer.

For some other reason I can not use QFutureWatcher() to get to know all the finished results.

There's no such reason.

Community
  • 1
  • 1
Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313