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();
}
int main(int argc, char *argv[])
{
Th1 *t = new Th1();
t->start();
Th2 *h = new Th2();
h->start();
}
To copy to clipboard, switch view to plain text mode
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;
}
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;
}
To copy to clipboard, switch view to plain text mode
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
.
Bookmarks