PDA

View Full Version : Threads or Event loop in multiplayer hangman game



TheIndependentAquarius
13th February 2014, 14:28
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?

wysota
13th February 2014, 14:43
You don't need any extra threads. Just implement the functionality and Qt will take care of the rest.

anda_skoa
13th February 2014, 15:12
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



while (true) {
Event *event = getEvent();
processEvent(event);
}




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,
_

TheIndependentAquarius
13th February 2014, 16:11
Thanks for the info.

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


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?

anda_skoa
13th February 2014, 19:36
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.



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.



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,
_

TheIndependentAquarius
1st March 2014, 08:03
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?

ChrisW67
1st March 2014, 08:33
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.