PDA

View Full Version : A while(1) C function to run in Qt



020394
19th July 2013, 03:47
Hello Guy,
I trying to put a while(1) C function in my Qt project.
I want to run it in the background of a specific Gui.
I have been trying to use threads to do it .I tried declaring the thread in main() and my Gui cpp file(infuison.cpp) But there seem to be a problem.
this is what i have done.
QThread: Destroyed while thread is still running
ASSERT failure in QThread::setTerminationEnabled(): "Current thread was not started with QThread.", file thread\qthread_win.cpp, line 574
Invalid parameter passed to C runtime function.
Invalid parameter passed to C runtime function.



//threading.h
#ifndef THREADINGS_H
#define THREADINGS_H

#include <QObject>
#include <QThread>

class Threadings : public QObject
{
Q_OBJECT
public:
explicit Threadings(QObject *parent = 0);
void DoSetup(QThread &cThread);
signals:

public slots:
void runAlgoMain();

};

#endif // THREADINGS_H




//threadings.cpp

#include "threadings.h"

extern "C++"
{
#include "test.h"
}

Threadings::Threadings(QObject *parent) :
QObject(parent)
{
}

void Threadings::DoSetup(QThread &cThread)
{
connect(&cThread,SIGNAL(started()),this,SLOT(runAlgoMain()) );
}

void Threadings::runAlgoMain()
{
runAlgoMain();
}




#include infusion.cpp
#include "threadings.h"
Infusion::Infusion(QWidget *parent) :
QWidget(parent),
ui(new Ui::Infusion)
{
QThread cThread;
Threadings cThreadings;
cThreadings.DoSetup(cThread);
cThreadings.moveToThread(&cThread);
cThread.start();
}


Am i doing threading right ? Please help :)

ChrisW67
19th July 2013, 06:35
Am i doing threading right ?
Clearly not.
What is the lifetime of the QThread object at line 7 of your last listing?

See How To Really, Truly Use QThreads; The Full Explanation (http://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/)

020394
19th July 2013, 07:13
So i have to make an instance of a new qthread and put my function into it?
But how am i going to move my Function into the thread ? eg Worker *worker=new Worker . How to pass when my Function is an .c not a c++?
Anyway can we use QTimer to make my c functions run in the background just like threads ?
I am unsure of the Puclic slot :finished ();, from the link you gave me .

wysota
19th July 2013, 07:22
C++ compilers don't care if you call "C" or "C++" functions in your programs.

020394
19th July 2013, 07:33
C++ compilers don't care if you call "C" or "C++" functions in your programs.

Oh wait, My "C" functions are in a .C file . So i am unable to do Worker *worker=new Worker at all .

ChrisW67
19th July 2013, 08:57
Wrap your plain functions in a class and the problem is solved. It really is not complicated. Here is a complete, threaded program that prints powers of two:


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

// Plain function with C linkage declaration
extern "C" {
int multiplyByTwo(int i);
}

// Plain function impl
int multiplyByTwo(int i) {
return i * 2;
}



class Worker : public QObject {
Q_OBJECT

public:
Worker();
~Worker();
public slots:
void process();
signals:
void finished();
private:
// add your variables here
};

Worker::Worker() { }
Worker::~Worker() { }

// Start processing data.
void Worker::process() {
int value = 1;
while (value < 1000000000) {
qDebug() << value;
value = multiplyByTwo(value);
}
emit finished();
}


int main(int argc, char **argv) {
QCoreApplication app(argc, argv);

QThread* thread = new QThread;
Worker* worker = new Worker;
worker->moveToThread(thread);
QObject::connect(thread, SIGNAL(started()), worker, SLOT(process()));
QObject::connect(worker, SIGNAL(finished()), thread, SLOT(quit()));
QObject::connect(worker, SIGNAL(finished()), worker, SLOT(deleteLater()));
QObject::connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
thread->start();

QObject::connect(thread, SIGNAL(destroyed()), qApp, SLOT(quit()));
return app.exec();
}
#include "main.moc"

020394
19th July 2013, 09:53
Thanks guys i have found another way to "Threading ", maybe wrongly executed .
I did it by declaring the "C" function into a function in the cpp file .
Then i used a Qtimer to connect the timer to the function and start it.

wysota
19th July 2013, 10:36
Thanks guys i have found another way to "Threading ", maybe wrongly executed .
I did it by declaring the "C" function into a function in the cpp file .
Then i used a Qtimer to connect the timer to the function and start it.

There is no threading here, timers do not spawn new threads.

wiz29
19th July 2013, 12:30
Try to use the QtConcurrent::run or the QtConcurrent::map instead the QThread.

020394
22nd July 2013, 02:12
So when can I use the QTimer ? I am trying to use the QTimer to run a function along with the Gui. Is it the right way to ?

wysota
22nd July 2013, 07:13
A timer is for running some code in a specific moment in time. It is handled by an event like (almost) everything else in Qt.

020394
22nd July 2013, 09:35
A timer is for running some code in a specific moment in time. It is handled by an event like (almost) everything else in Qt.

Isnt It the same as running threads ? For now i am doing a school project with different groups all doing different part . for me i am doing the Gui. I need to integrate the rest of the codes my friends do . EG (Algorithm , Checking of a keypad. etc). Am i able to use QTimer for them since i want them to run at a specific moment of time when i am at the Gui page

wysota
22nd July 2013, 09:51
Isnt It the same as running threads ?
No. If you are to eat lunch at 12 o'clock, it doesn't mean at 12 some other instance of yourself is spawned and eats a sandwich but rather you have to stop what you had been doing, take out a sandwich, unwrap it, eat it and then attend your other tasks.

020394
22nd July 2013, 10:07
I see so threading is always running in the background . But i been using timer to check the current time(Updatiing the time on the Label) , Isnt that considered as threadings?

wysota
22nd July 2013, 11:25
I see so threading is always running in the background . But i been using timer to check the current time(Updatiing the time on the Label) , Isnt that considered as threadings?

No. Timers have nothing to do with threading.