Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QtConcurrent::run() causes QWaitCondition destroyed messages

QtConcurrent::run() causes QWaitCondition destroyed messages

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 5.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    adnan
    wrote on last edited by
    #1

    I’m trying to run a function of a class using the QtConcurrent::run() method. The thread runs and does exactly what it should. However, when the thread ends, I get this error message in the console:

    @QWaitCondition: Destroyed while threads are still waiting@

    It works fine on Linux without any error message, but application crashes in Windows when i close the application

    Here is the code of main function:
    @ int main(int argc, char *argv[])
    {
    QtSingleApplication a(argc, argv);

        if(a.isRunning()&&argc==2)
        {
            a.sendMessage(argv[1]);
            return 0;
        }
       
        else if (a.isRunning())
        {
              return 0;
        }
     
        testApp w;
     
        testApp.show();
     
        a.setQuitOnLastWindowClosed(false);
        if(argc==2)
        {
            w.showDialog(argv[1]);
        }
     
        QObject::connect(&a, SIGNAL(messageReceived(const QString&)),
                         &w, SLOT(showDialog(QString)));
     
     
     
        return a.exec();
    }
    

    @

    1 Reply Last reply
    1
    • V Offline
      V Offline
      vezprog
      wrote on last edited by
      #2

      Probably need to see more of the code to figure out what is going on. How do you create your thread? I am assuming your thread gets generated in your testApp class.

      What is it saying is that your thread is not being cleaned up or killed properly before your 'parent' class is being deleted. You have a wait condition that is still probably being hit while your main is trying to return from its run loop and delete all of its children (the thread).

      If your in a run loop of some sort, call exit() on the thread class before you clean everything up in your 'parent' class. Or pop out of your while in your run() function within the thread (many ways to do this depending on how your using the thread) and back out of the wait condition.

      1 Reply Last reply
      1
      • A Offline
        A Offline
        adnan
        wrote on last edited by
        #3

        Thanks for the reply. Here is the code:
        @void MyClass::ready()
        {
        QtConcurrent::run (this,&MyClass::start);
        }

        void MyClass::start()
        {

        // do something
        emit
        done();
        }@

        After, the done() signal is emitted, if i close the application, the application crashes, but if i wait for half a minute or so, and then close the application, it works fine.
        I just want to run a single function of class in a separate thread.

        1 Reply Last reply
        1
        • V Offline
          V Offline
          vidar
          wrote on last edited by
          #4

          Just because you emit a done() signal does not mean that the thread has been closed properly.

          What you will probably need to do is to use the return value of QtConcurrent:run (which is a QFuture class) and check for QFuture:result(). QFuture:result() will block until the thread is done. You could also use QFuture::waitForFinished if you don't have a return value.
          Forget the "signal when thread is done" method, this is a really dangerous approach.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            adnan
            wrote on last edited by
            #5

            I used @future.waitForFinished()@ but now the application hangs, if i try to close it.
            @ void MyClass::ready()
            {
            future = QtConcurrent::run (this,&MyClass::start);
            }

            void MyClass::start()
            {
             
              // do something
              emit
                   done();
              future.waitForFinished()
            }
            

            @

            1 Reply Last reply
            1
            • V Offline
              V Offline
              vidar
              wrote on last edited by
              #6

              No, that's the wrong place. With your current code the thread waits for its own execution to be finished (what is not very likely, as it is busy with waiting for its own execution to be finished ;-D )

              @ void MyClass::ready()
              {
              future = QtConcurrent::run (this,&MyClass::start);
              future.waitForFinished() // maybe do this somewhere outside (in your main class)
              }

              void MyClass::start()
              
              {
                // do what needs to be done
              }@
              
              1 Reply Last reply
              1

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved