PDA

View Full Version : QThread don' stop (or the signal don't emit)



msdark
10th March 2011, 22:13
Hi (sorry for my english).. i'm new in the forum, and this is my first post,(shame.. is a question) .. I'm working in a console application that use OpenGL to show a Camera view, and i add Qt to use the really awesome class QThread (C++) ..But i have some problems.. here my code and then the description:


#ifndef CONTADOR_REGISTER_H
#define CONTADOR_REGISTER_H

#include <QThread>
#include <QObject>
#include <QDebug>

#include "database.h"

class ContadorRegister : public QThread
{
Q_OBJECT
public:
ContadorRegister(User user, UserID id);
void run();
private:
User user;
UserID uid;
public slots:
void fin(){ qDebug()<<"Thread ended"; }

};

#endif // CONTADOR_REGISTER_H

//And the implementation in contador_register.cpp
#include "contador_register.h"

ContadorRegister::ContadorRegister(const User userG, UserID id){
this->uid = id;
this->user = userG;
}


void ContadorRegister::run(){
this->setTerminationEnabled(true);
QObject::connect(this,SIGNAL(finished()),this,SLOT (fin()));
int dir=0;
//Some calculations
//Registro en BD
if(dir==1){
QString sql="INSERT INTO registro VALUES(";
sql.append("NOW())");
DB->insertar(sql);
}
this->finished();
}


And finally the function where i call this thread.
This is a callback function .. this function trigger up every time of a user is saw for the camera.



ContadorRegister* cr = new ContadorRegister(generator,nId);
cr->start();
qDebug()<<"Starting" << cr->currentThreadId();

if(cr->isRunning()){
qDebug()<<"Still Running "<<cr->currentThreadId();
}else
{
qDebug()<<"Ended "<<cr->currentThreadId();
}


Well .. the problem is.. i this function (or another in the main) i need to check if the QThread is running or not...

And beside.. if the thread don't die.. the memory consume will be big... this application will run 24hrs 7days..

Any idea?

Thanks

wysota
10th March 2011, 23:51
First of all currentThreadId() returns something else than you think. As the name suggests it returns the CURRENT THREAD id, not the id of the thread you're calling the method on (it's a static method so the caleee instance is irrelevant). Second of all if your callback function is represented by the second snippet of yours then it will not work and it doesn't have sense to be working in this configuration. If you spawn a thread and do nothing while waiting for it to finish (assuming you would actually wait in your code and not just check if the thread was running) then there is no benefit in having the thread in the first place.