Results 1 to 7 of 7

Thread: Threads or Event loop in multiplayer hangman game

  1. #1
    Join Date
    Apr 2011
    Posts
    231
    Thanks
    141
    Thanked 6 Times in 5 Posts

    Default Threads or Event loop in multiplayer hangman game

    I wish to implement a simple multiplayer Hangman game with the rule slightly bended.

    Rule: All the players have to guess the alphabets in the word at the same time. Whoever player guesses a correct alphabet gets a point, and the player who puts the ending alphabet gets bonus points. This will be about speed. The faster correct guess gets you ahead of others.

    I intend to have a Qt/QML based GUI and the programming language will be C++. Platform will be Linux.

    I am thinking I'll need:
    - One thread for handling user typing.
    - Second thread for parallel display of what other players are typing.
    - Third thread for parallel display of scores of every player on every player's screen.

    Do I need these 3 threads or I am barking the wrong tree?

    I posted this question on another site and got following reply:
    No you don't need threads for such lightweight tasks. You also don't need Boost for a typical Qt application. Qt is a complete (some would say bloated) framework, and Qt::Application has an event loop, so make use of it.
    What is an event loop? do I need that?

  2. #2
    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: Threads or Event loop in multiplayer hangman game

    You don't need any extra threads. Just implement the functionality and Qt will take care of the rest.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Threads or Event loop in multiplayer hangman game

    Quote Originally Posted by Anisha Kaul View Post
    What is an event loop?
    An event loop is a construct where a thread runs in a loop, in which it waits for events. Whenever it gets any event, that could be user interaction or timers or data arriving at sockets, etc, the thread will process them and finally return to waiting.

    Basically something like

    Qt Code:
    1. while (true) {
    2. Event *event = getEvent();
    3. processEvent(event);
    4. }
    To copy to clipboard, switch view to plain text mode 

    Quote Originally Posted by Anisha Kaul View Post
    do I need that?
    You already have that as part of your Qt application object. You start the event loop when you code in main() calls exec().
    This main thread event loop handles all the UI events, but can of course also handle others.

    Cheers,
    _

  4. The following user says thank you to anda_skoa for this useful post:

    TheIndependentAquarius (1st March 2014)

  5. #4
    Join Date
    Apr 2011
    Posts
    231
    Thanks
    141
    Thanked 6 Times in 5 Posts

    Default Re: Threads or Event loop in multiplayer hangman game

    Thanks for the info.

    But still don't I need threads which a server will maintain to handle all the client players?

    Quote Originally Posted by wysota View Post
    You don't need any extra threads. Just implement the functionality and Qt will take care of the rest.
    If I get out of Qt, then will I need the threads mentioned in the first post?

    What do I need to study to know how Qt handles everything on its own?

  6. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Threads or Event loop in multiplayer hangman game

    Quote Originally Posted by Anisha Kaul View Post
    But still don't I need threads which a server will maintain to handle all the client players?
    No, because Qt's socket classes are also event based.

    Quote Originally Posted by Anisha Kaul View Post
    If I get out of Qt, then will I need the threads mentioned in the first post?
    Depends on what other library or API you would be using then.

    Quote Originally Posted by Anisha Kaul View Post
    What do I need to study to know how Qt handles everything on its own?
    I am sure there are some articles, blogs or videos that go into the details of that, but you usually don't need to know them.
    All you need to know is how to deal with signals&slots.

    Cheers,
    _

  7. The following user says thank you to anda_skoa for this useful post:

    TheIndependentAquarius (1st March 2014)

  8. #6
    Join Date
    Apr 2011
    Posts
    231
    Thanks
    141
    Thanked 6 Times in 5 Posts

    Default Re: Threads or Event loop in multiplayer hangman game

    Quote Originally Posted by anda_skoa View Post
    No, because Qt's socket classes are also event based.
    _
    Thanks for replying.


    Actually, my interest here is to build a software on Qt which uses threads. The reason is that I want to practice using threads.
    So, is there a kind of project which would require threads when developed with Qt?

  9. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Threads or Event loop in multiplayer hangman game

    You can use threads to do what you describe but Qt does not require you to use threads. There is a multithreaded server example in the documentation. That might be sufficient as a learning exercise.

Similar Threads

  1. Replies: 4
    Last Post: 6th August 2011, 02:40
  2. Replies: 10
    Last Post: 15th January 2010, 15:35
  3. Replies: 0
    Last Post: 23rd October 2008, 13:43
  4. signal and slot across threads having event loop
    By travis in forum Qt Programming
    Replies: 6
    Last Post: 6th November 2007, 00:56
  5. Making 3D controls in QT, implementing a game loop in QT
    By smurrish in forum Qt Programming
    Replies: 10
    Last Post: 26th April 2006, 05:37

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.