Results 1 to 10 of 10

Thread: Qt thread problem

  1. #1
    Join Date
    Aug 2010
    Location
    Kandy, Sri Lanka
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Qt thread problem

    whats wrong with this??

    Qt Code:
    1. #include <iostream>
    2. #include <QThread>
    3.  
    4. using namespace std;
    5.  
    6.  
    7. class Th1 : public QThread
    8. {
    9. public:
    10. void run()
    11. {
    12. while (true)
    13. {
    14. cout << "X" << endl;
    15. msleep(500);
    16. }
    17. }
    18. };
    19.  
    20.  
    21. class Th2 : public QThread
    22. {
    23. public:
    24.  
    25. void run()
    26. {
    27. while (true)
    28. {
    29. cout << "Y" << endl;
    30. msleep(500);
    31. }
    32. }
    33. };
    34.  
    35.  
    36. int main(int argc, char *argv[])
    37. {
    38. Th1 *t = new Th1();
    39. t->start();
    40.  
    41. Th2 *h = new Th2();
    42. h->start();
    43. //h->run();
    44. }
    To copy to clipboard, switch view to plain text mode 

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

  2. #2
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt thread problem

    a strange problem .... start will automatically initate the run() function ... might be the problem with protected ..?
    "Behind every great fortune lies a crime" - Balzac

  3. #3
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt thread problem

    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.
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Qt thread problem

    start calls automatically run and
    Qt Code:
    1. #include <QtGui>
    2.  
    3.  
    4. class Th1 : public QThread
    5. {
    6. public:
    7. void run()
    8. {
    9. while (true)
    10. {
    11. qWarning() << "X" << endl;
    12. msleep(500);
    13. }
    14. }
    15. };
    16.  
    17.  
    18. class Th2 : public QThread
    19. {
    20. public:
    21.  
    22. void run()
    23. {
    24. while (true)
    25. {
    26. qWarning() << "Y" << endl;
    27. msleep(500);
    28. }
    29. }
    30. };
    31.  
    32.  
    33. int main(int argc, char *argv[])
    34. {
    35. QApplication a(argc, argv);
    36.  
    37.  
    38. Th1 *t = new Th1();
    39. t->start();
    40.  
    41. Th2 *h = new Th2();
    42. h->start();
    43.  
    44.  
    45. return a.exec();
    46. }
    To copy to clipboard, switch view to plain text mode 
    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.

  5. #5
    Join Date
    Aug 2010
    Location
    Kandy, Sri Lanka
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Qt thread problem

    thnx for the replies guys... Lykurg, your program worked... I guess I have to use a return statement in the end..

  6. #6
    Join Date
    Aug 2010
    Location
    Kandy, Sri Lanka
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Qt thread problem (The reason)

    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).
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. Th1 *t = new Th1();
    4. t->start();
    5.  
    6. Th2 *h = new Th2();
    7. h->start();
    8. }
    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.
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. Th1 *t = new Th1();
    4. t->start();
    5.  
    6. Th2 *h = new Th2();
    7. h->start();
    8. //read a char from console
    9. char tem;
    10. cin>>tem;
    11. }
    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 .
    Last edited by sinaru; 18th August 2010 at 04:19.

  7. #7
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Qt thread problem

    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.

  8. #8
    Join Date
    Aug 2010
    Location
    Kandy, Sri Lanka
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Qt thread problem

    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

  9. #9
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Qt thread problem

    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)

  10. #10
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qt thread problem

    Quote Originally Posted by sinaru View Post
    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();

Similar Threads

  1. Gui and non-Gui thread problem
    By AviMittal in forum Qt Programming
    Replies: 1
    Last Post: 21st September 2009, 13:03
  2. thread problem
    By talk2amulya in forum Qt Programming
    Replies: 21
    Last Post: 18th February 2009, 12:05
  3. GUI and non-GUI thread problem
    By dimaz in forum Qt Programming
    Replies: 3
    Last Post: 18th September 2008, 21:25
  4. Thread problem
    By panos in forum Newbie
    Replies: 9
    Last Post: 14th September 2007, 19:32
  5. Thread Problem
    By qball2k5 in forum Qt Programming
    Replies: 2
    Last Post: 12th April 2006, 17:31

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.