QThread emits finished() signal but isRunning() returns true and isFinished() returns false
Asked Answered
F

3

11

Below is the code for my qthread implementation. I am trying to get gps data from satellite. QThread doesn't produce the finished() signal even when the programs exits gpsSearch() slot function. The function locateMe() is called whenever a button is clicked. The first time when the thread isnt started and the button is clicked it prints true value for isRunning() function and prints false value for isFinished() function. I had to call the quit() function of the QTherad to manually stop the thread. After that it goes to the connected threadQuit() function in the gnssProvider class. But even after that if I click the button it prints true value for isRunning and false for isFinished() in the locateMe() function.

GPSInfo::GPSInfo()
{
    hybridGPSFound = satelliteGPSFound = networkGPSFound = false;
    qDebug()<<"Thread Creating";
    gnssThread = new QThread;
    gnssProvider = new LocationFetcher(this,GEOLOCATION_PROVIDER_GNSS,1);
    gnssProvider->moveToThread(gnssThread);
    connect(gnssThread, SIGNAL(started()), gnssProvider, SLOT(gpsSearch()));
    connect(gnssThread, SIGNAL(finished()), gnssProvider, SLOT(threadQuit()));
}
void LocationFetcher::gpsSearch()
{
    if (BPS_SUCCESS != geolocation_request_events(0))
    {
       fprintf(stderr, "Error requesting geolocation events: %s", strerror(errno));
       return;
    }
    geolocation_set_provider(GPS_Search_Provider);
    geolocation_set_period(GPS_Search_Period);
    while (!stopThread)
    {
        bps_event_t *event = NULL;
        bps_get_event(&event, -1);

        if (event)
        {
            if (bps_event_get_domain(event) == geolocation_get_domain() && bps_event_get_code(event) == GEOLOCATION_INFO)
            {
                handle_geolocation_response(event);
                break;
            }
        }
    }
    geolocation_stop_events(0);

    this->quit();

}
void GPSInfo::LocateMe()
{
    qDebug()<<"Thread Running: "<<gnssThread->isFinished();
    qDebug()<<"Thread Running: "<<gnssThread->isRunning();

    gnssThread->start();
    hybridThread->start();
    networkThread->start();

 }
Fasciation answered 11/6, 2013 at 13:30 Comment(5)
Are you sure that you didn't mix up isFinished and isRunning? You have "Thread Running: " in both lines.Vespine
No I didn't :) I was too lazy to fix that :PFasciation
can you post full example? Maybe thread didn't finishAbdu
I would suggest trying to reduce the code to a minimal example that compiles but still shows the problem. It will help you to pinpoint what is the issue, and us to provide an answer (since threads can be tricky and seeing exactly where/when you call the functions is useful).Polychaete
What is the question, exactly?Bannasch
V
42

The way the QThread lifecycle works is like this:

  1. You call QThread::start().
  2. At this point, isRunning() should start returning true.
  3. The thread internals start. They emit the started() signal.
  4. The thread internals call run().
  5. Unless you override this in a subclass, run() calls exec().
  6. exec() enters an event loop and stays there until quit() or exit() is called.
  7. exec() and run() return to the internals.
  8. At this point, isFinished() should start returning true and isRunning() false.
  9. The internals emit the finished() signal.
  10. The internals do some final cleanups.
  11. The thread terminates for real.

So you need to call quit() after your location fetcher is done - but this->quit() isn't calling quit() on the thread! This is probably why it's not doing anything.

Your code looks a bit like it was patterned after this article:

http://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/

Note how she gives her worker a finished() signal (not the same as QThread::finished) and connects it to the QThread::quit() slot.

Vibraculum answered 13/6, 2013 at 18:26 Comment(4)
This post is the correct answer. I would only add that we need to know more about the threadQuit slot to say more. If that slot deletes the thread, sounds to me like you might have a dangling pointer.Bannasch
Thanks a lot that gives me a better understanding about QThread :)Fasciation
A very comprehensive answer, difficult to find elsewhere.Faires
Absolutely. And don't expect that isRunning () will return false immediately after you called quit (), as the event loop may not have picked up on it yet.Pyriform
B
2

It's intended behavior that the thread doesn't quit until you manually terminate it. The thread object hosts an event loop, so it doesn't Finish until the event loop quits, as Sebastian explained.

In short, your signal-slot connections are backwards conceptually - the object should terminate the thread when it is finished doing its thing, not the other way around.

Bannasch answered 17/6, 2013 at 18:7 Comment(0)
O
0

Which Qt version do you use?

Qt 4.8 returned wrong values until 4.8.4 (Qt bug 30251). This bug has been fixed in 4.8.5.

Olivo answered 19/4, 2017 at 14:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.