Results 1 to 11 of 11

Thread: QThread usage and exec()

  1. #1
    Join Date
    Aug 2008
    Posts
    14
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default QThread usage and exec()

    I have some questions regarding the usage of QThread, and I'm hoping someone here can give me a couple pointers. I've read the documentation and the examples on the usage of QThread, but there are some finer points that I must be missing.

    First, some background. I'm writing a multi-player puzzle game. The project is being tackled in two pieces: a game server, and a game client. The server will handle all game control and logic, and will have only rudimentary visual display (printf statements to a console... maybe). The client will communicate with the server via TCP/IP and will be responsible for the GUI aspects of the game.

    The server class inherits QThread directly. The game flow is such that the server is basically a large state machine, so it doesn't do anything (except track a timer) unless messages come in from the clients. When a message comes in, the server parses it and takes appropriate action. I'd like the server to take as little time as possible so that there's plenty of CPU left for the graphical aspects of the client.

    Here's where the questions begin -

    1) The documentation states that in order for the thread to handle events, I need to invoke exec(). Do I need to invoke exec() repeatedly (eg in a while( !gameOver ) loop), or just once?

    2) Does exec() return immediately when invoked? That is, when does code execution in the thread continue?

    3) The documentation mentions that when run() exits the thread stops execution. Am I correct in believing that this would render the thread incapable of handling events? I guess the real question is whether or not I need a captive loop in the run() routine.

    Thanks in advance for any help!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QThread usage and exec()

    Quote Originally Posted by smahnken View Post
    Do I need to invoke exec() repeatedly (eg in a while( !gameOver ) loop), or just once?
    Once is enough.

    Quote Originally Posted by smahnken View Post
    Does exec() return immediately when invoked? That is, when does code execution in the thread continue?
    The docs say:
    Enters the event loop and waits until exit() is called, returning the value that was passed to exit(). The value returned is 0 if exit() is called via quit().
    Which means that it won't return until you invoke QThread::exit().

    Quote Originally Posted by smahnken View Post
    Am I correct in believing that this would render the thread incapable of handling events?
    No, as long the event loop runs, the thread will process events.

  3. The following user says thank you to jacek for this useful post:

    smahnken (21st August 2008)

  4. #3
    Join Date
    Aug 2008
    Posts
    14
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QThread usage and exec()

    Thanks for the help!

    Another question for the group:

    My client acts like it's not getting any time (graphics don't refresh, timed messages never disappear, etc.). Does the event loop yield control when there's no messages, or does it hold onto it? I need it to essentially sleep until messages come in... Is there something else I need to do (set the thread priority lower, perhaps?).

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QThread usage and exec()

    The event loop runs in a ... loop until the application ends (or actually until exit() is called which most of the times means the same). It doesn't prevent your application from redrawing itself - on the contrary, your widgets couldn't refresh themselves if the loop was not running. The whole point of event driven technologies is that the flow remains with the event loop all the time and that the loop decides what gets executed and when based on events that are generated.

  6. #5
    Join Date
    Aug 2008
    Posts
    14
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QThread usage and exec()

    Forgive me, but I must be missing something in the way things work here.

    My main application is a client (which, of course, has its own event loop). When a game is started, the client launches a separate thread for the server to run in. Said thread executes exec(), giving it an event loop. Do I not now have two event loops running? If so, does the operation of one conflict with the other? That seems to be the way it's behaving, but perhaps I am misinterpreting it.

    My intent is to have two separate threads operating independently, one for the server and one for the client. Each needs to handle messages and events without conflicting with each other. Is that possible using the "client launches the server in a separate thread" approach, or do I need to code the two entities as separate apps and execute them independently of each other (like the fortune server/client examples)?

    Sorry for the confusion!

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QThread usage and exec()

    Quote Originally Posted by smahnken View Post
    Do I not now have two event loops running?
    Yes, that's correct.

    If so, does the operation of one conflict with the other?
    No, they do not interfere each other. Each of them processes events for objects created (and owned) by their thread.

    My intent is to have two separate threads operating independently, one for the server and one for the client.
    This might not be necessary, you know...

    Each needs to handle messages and events without conflicting with each other. Is that possible using the "client launches the server in a separate thread" approach, or do I need to code the two entities as separate apps and execute them independently of each other (like the fortune server/client examples)?
    You can do it in a single processes. It is likely you can do it even in a single thread with a single event loop.

  8. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QThread usage and exec()

    Quote Originally Posted by smahnken View Post
    My client acts like it's not getting any time (graphics don't refresh, timed messages never disappear, etc.).
    Are you sure you start a separate thread?

  9. #8
    Join Date
    Aug 2008
    Posts
    14
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QThread usage and exec()

    I guess I'm not expert enough to say that I'm certain, but I'm pretty sure. Perhaps you can provide a sanity check for me...

    The server class is declared like so:
    Qt Code:
    1. class Xorbz_Game : public QThread
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. Xorbz_Game( class Xorbz* parent );
    7. ~Xorbz_Game( void );
    To copy to clipboard, switch view to plain text mode 
    etc...

    The client (main app) instantiates the server like so:
    Qt Code:
    1. // Instantiate a new game engine
    2. currentGame = new Xorbz_Game( this );
    To copy to clipboard, switch view to plain text mode 

    After the client sets a few things in the server via member functions, it starts things up like this:
    Qt Code:
    1. // Start the game running
    2. currentGame->start();
    To copy to clipboard, switch view to plain text mode 

    Anything look amiss?

  10. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QThread usage and exec()

    Quote Originally Posted by smahnken View Post
    Anything look amiss?
    No, you must block the main event loop in some other way. Do you have any loops in your GUI code?

  11. #10
    Join Date
    Aug 2008
    Posts
    14
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QThread usage and exec()

    No loops in my GUI code, but maybe I'm chasing the wrong thing. I'm probably doing something wrong with the graphics. I found that if I resize the window, or minimize/restore it, the graphics do update, but only while I'm resizing.

    The whole project is a migration effort from the QT 3.x series, in which the code base used the QCanvas and QPixmapArray stuff. I'm now trying to establish the same functionality with the QT 4.x series.

    I'll go poke around (and read) some more with the new graphics structure. I know there's the whole update() thing, but there's a good chance I've got something wrong there. I'll start a new discussion thread once I've got some substantiated questions.

    Thanks again for your help!

  12. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QThread usage and exec()

    Are you sure you need threads at all?

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.