PDA

View Full Version : Qt thread problem



sinaru
17th August 2010, 06:21
whats wrong with this??


#include <iostream>
#include <QThread>

using namespace std;


class Th1 : public QThread
{
public:
void run()
{
while (true)
{
cout << "X" << endl;
msleep(500);
}
}
};


class Th2 : public QThread
{
public:

void run()
{
while (true)
{
cout << "Y" << endl;
msleep(500);
}
}
};


int main(int argc, char *argv[])
{
Th1 *t = new Th1();
t->start();

Th2 *h = new Th2();
h->start();
//h->run();
}


when i uncomment the last statement, it works...
im confused.

wagmare
17th August 2010, 06:25
a strange problem .... start will automatically initate the run() function ... might be the problem with protected ..?

franz
17th August 2010, 07:39
This is exactly as it should be. You re-implement a protected run() function which is automatically started when you call start(). If you would be calling run() from your creating thread, you would be running that code in the calling thread instead of in your new thread. That would be silly. If you need a function that can be used from both threads, implement a function that is called from run() and called from the main thread as necessary.

Lykurg
17th August 2010, 07:45
start calls automatically run and
#include <QtGui>


class Th1 : public QThread
{
public:
void run()
{
while (true)
{
qWarning() << "X" << endl;
msleep(500);
}
}
};


class Th2 : public QThread
{
public:

void run()
{
while (true)
{
qWarning() << "Y" << endl;
msleep(500);
}
}
};


int main(int argc, char *argv[])
{
QApplication a(argc, argv);


Th1 *t = new Th1();
t->start();

Th2 *h = new Th2();
h->start();


return a.exec();
}
works for me. Also msleep and while(true) are not the best solution, better use a timer with a custom slot where you do your stuff and leave run "empty".

EDIT: too late.

sinaru
17th August 2010, 09:19
thnx for the replies guys... Lykurg, your program worked... I guess I have to use a return statement in the end..

sinaru
18th August 2010, 04:03
Lykurg, I have some questions. u said it's better to leave run empty. I was referering the QThread page on Nokia Qt website,
Link: http://doc.qt.nokia.com/4.6/qthread.html#details
On that page, they have said, "To create your own threads, subclass QThread and reimplement run(). ". Also they have provided an example too. I wanted to just do something to test Qthread class. So that's why I used a while(true) staement and a sleep statement to see the output much clearly. :). Hence I only needed to include QThread class from Qt framework. Im kinda new to Qt(around a week) but have some knowledge of c++(I love c++ because its the only language I think know :P). thought to use this framework to further program in c++. So my question is that whether it is bad to include Qclasses for simple c++ programs??

Also I was thinking about the code that I have first put. I finally get what happened,in the morning it just randomly came to my head :P.

Note the main function(forget the last comment).

int main(int argc, char *argv[])
{
Th1 *t = new Th1();
t->start();

Th2 *h = new Th2();
h->start();
}

It has only two thread start statements and after h->start(), there is a hidden return statement. So when the execution temporarily comes back to main, it executes the next statement which is a return statement(usually returns 0), causing the program to terminate. So I added two statements to main to test it again.

int main(int argc, char *argv[])
{
Th1 *t = new Th1();
t->start();

Th2 *h = new Th2();
h->start();
//read a char from console
char tem;
cin>>tem;
}

The cin statement caused the program to wait until user input something. Similar to the last statment of Lykurg (a.exec()).
ie. exec enters the main event loop and waits until exit() is called.
Link: http://doc.qt.nokia.com/4.6/qapplication.html#exec

So on the console, x and y were printing, and at the same time user can enter characters and if the user press some keys and then press enter, the program terminates :D.

tbscope
18th August 2010, 05:44
Please, when creating a Qt program, always use an Q...Application object and start an event loop.

By the way, there is no such thing as a hidden return in C++. At the end of a scope, you return from that scope. It's as basic as that. It's up to you to provide the correct return values. If not, the compiler WILL complain. And if you don't set this warning as an error, your program will crash when you do this often. It's bad technique to not control your return values.

sinaru
18th August 2010, 07:33
well I was talking about main function u see. you can omit the return statement from main. If you do, and main finished, there is an implicit return 0. That's why I said a hidden return statement. :P

Lykurg
18th August 2010, 07:35
I haven't said to leave the run() empty. Have have said to leave it "empty". So only initiate there a timer which periodically calls a slot of your thread. This is one way to avoid while(true) and sleep. (In the slot put everything you do in the while scope)

squidge
18th August 2010, 07:46
well I was talking about main function u see. you can omit the return statement from main. If you do, and main finished, there is an implicit return 0. That's why I said a hidden return statement. :P

That depends on the compiler. Some will happily return garbage if you miss out an explicit return statement, which, for portability, is why you should always use one.

Since main is the perfect place to start an event loop also, the best practice is to return <somevar>.exec();