Kenny
Kenny

Reputation: 4572

C++ Qt5 - Program finishes before QThread

Let's take a look at the following code:

class CommandRetriever
{
    public:
        CommandRetriever();
        ~CommandRetriever();
    
        void addCommand( QString, Command* );
        void executeCommands();

    private:
        QMap<QString, Command*> m_commands;
};

addCommand should be self-explanatory; it will add a new entry into m_commands. However, let's take a look at the implementation of executeCommands...

void CommandRetriever::executeCommands()
{
    for( auto iter : m_commands.keys() )
    {
        Command *cmd = m_commands.value( iter ); 
         
        // Command, password //
        RemoteConnection *rcon = new RemoteConnection( cmd, "" );

        QThread *thread = new QThread();
        rcon->moveToThread( thread );
        QObject::connect( thread, SIGNAL( started() ), rcon, SLOT( doAsync() ) );
        QObject::connect( rcon, SIGNAL( finished() ), thread, SLOT( quit() ) );
        QObject::connect( rcon, SIGNAL( finished() ), rcon, SLOT( deleteLater() ) );
        QObject::connect( thread, SIGNAL( finished() ), thread, SLOT( deleteLater() ) );
        thread->start();
    }
}

My rcon object is a QObject which has the public slot doAsync() to do work off the main thread. All of this is per Qt official examples and what I was able to muster from various blogs.

This is a completely console-based program, so I have no Windows, Widgets, or event loops to work with.


The Problem

What happens is that my program will exit before any asynchronous work is able to be done (e.g. connect to a remote host, write data, etc). Sometimes, if I'm lucky, my threads will do work fast enough to output something, so I know that the threads are working as they should. My question is: How do I keep my main program running until my threads finish the work? Is there an official way of doing this in QThread? Because I haven't seen anything in the docs.

Thanks!


Addendum April 20, 2015

Although the accepted answer made by Jeremy in this question is correct, it makes less sense for console-based applications, where an event loop is typically not needed.

Thus, I asked a new question, and found an answer for those in search of using threads without Qt event loop non-sense:

QThread never quits due to QCoreApplication event loop

Upvotes: 1

Views: 403

Answers (1)

Jeremy Friesner
Jeremy Friesner

Reputation: 73294

The method you are looking for is QThread::wait(). Call it on each thread object, from your main thread, before starting your program's cleanup/exit stage. It will wait until the thread has exited before returning.

Upvotes: 3

Related Questions