[SOLVED] Permanent Thread



  • Hi,

    I've read the "Threading Basics":http://qt-project.org/doc/qt-5.0/qtcore/thread-basics.html

    Before I did successfully do a thread by subclassing QThread and using run(), but I read this is not good use. My Thread needs to be permanent (lifetime of my application). I want to retrieve data from it at various time.

    So now I want to implement this kind of Threa


  • Moderators

    Hi,

    That page is outdated. Those examples will be removed in the Qt 5.2 documentation.

    You don't need to implement your own thread for your purpose. Just create a new QThread(). See the first example in the "QThread documentation":http://doc-snapshot.qt-project.org/qt5-stable/qthread.html#details

    Also, see the "Multithreading Technologies in Qt":http://doc-snapshot.qt-project.org/qt5-stable/threads-technologies.html page for different ways to use threads in Qt -- subclassing QThread is ok for certain uses (but not for permanent threads like yours)



  • Thank you JKSH,

    The example supplied worked perfectly for my needs, now I need to figure out how to exchange data from the controller and the worker, I think signal and slot will do the job just fine :)

    @////////////////////////////////////////////////////////////////////////////////
    /// CONSTRUCTOR
    ////////////////////////////////////////////////////////////////////////////////
    DialogAnt::DialogAnt(QWidget *parent) :QDialog(parent),ui(new Ui::DialogAnt) {
    ui->setupUi(this);

    myHub = new Hub();  //cannot move object with a parent, so no parent and delete in destructor
    myHub->moveToThread(&workerThread);
    
    connect(myHub, SIGNAL(onStickFound()), this, SLOT(stickFound()) );
    connect(myHub, SIGNAL(onStickFound()), myHub, SLOT(Start()) );
    
    workerThread.start();
    myHub->InitUSBStick();
    

    }@


  • Moderators

    [quote author="maximus" date="1384266474"]now I need to figure out how to exchange data from the controller and the worker, I think signal and slot will do the job just fine :)[/quote]Yes :) Use signals and slots to communicate both ways.



  • Hi again,

    My thread is working fine but for terminating the thread, i'm facing some difficulties.

    When I try to close my main GUI, the app becomes "not responding" trying to shut down the thread.
    The only way I was able to shut it down is with workerThread.terminate() but then I get error at shutdown

    My Thread is also running a thread inside it.
    I tell it to stop with the flag "threadRunning" but still I get stop on the .wait in the main GUI.
    Any idea on how to force the thread to stop without having a crash?

    Thanks in advance!

    [EDIT: The problem is in the children thread, it is not stopped before I stop it's parent thread, Stopping the children thread then the parent thread worked]

    @#include "dialogant.h"
    #include "ui_dialogant.h"
    #include <QDebug>
    #include "util.h"

    ////////////////////////////////////////////////////////////////////////////////
    /// CONSTRUCTOR
    ////////////////////////////////////////////////////////////////////////////////
    DialogAnt::DialogAnt(QWidget *parent) :QDialog(parent),ui(new Ui::DialogAnt) {
    ui->setupUi(this);

    myHub = new Hub();  //cannot move object with a parent, so no parent and delete in destructor
    myHub->moveToThread(&workerThread);
    
    connect(myHub, SIGNAL(onStickFound()), this, SLOT(stickFound()) );
    connect(this, SIGNAL(pairHR()), myHub, SLOT(Start()) );
    connect(myHub, SIGNAL(hrChanged(int)), this, SLOT(show_hr(int)) );
    
    workerThread.start();
    myHub->InitUSBStick();
    

    }

    ////////////////////////////////////////////////////////////////////////////////
    /// DESTRUCTOR
    ////////////////////////////////////////////////////////////////////////////////
    DialogAnt::~DialogAnt() {
    workerThread.quit();
    workerThread.wait();
    delete myHub;
    delete ui;
    }

    ////////////////////////////////////////////////////////////////////////////////
    /// CloseEvent
    ////////////////////////////////////////////////////////////////////////////////
    void DialogAnt::closeEvent(QCloseEvent *event) {

    qDebug() << "CLOSING WINDOW";
    

    // disconnect(myHub, SIGNAL(onStickFound()), this, SLOT(stickFound()) );
    // disconnect(this, SIGNAL(pairHR()), myHub, SLOT(Start()) );
    // disconnect(myHub, SIGNAL(hrChanged(int)), this, SLOT(show_hr(int)) );

    // myHub->Close();

    // workerThread.terminate(); works but shows error when app close
    myHub->threadRunning = false;
    myHub->Close();
    workerThread.quit();
    workerThread.wait();
    }@


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.