The goal is to develop something similar with a quiz application: there is a server to which players connect. At some point, a game is started. The game consists in asking the players a few multiple answer questions and determining the winner based on some criteria.

The server spawns a new thread for each client (I know it might be better and easier to use select, but the point is to understand/learn multithreading). The users are "pending" until a sufficient number of users are available such that the game can start.

The problem is what happens when the game starts? My current best approach is to use a lot of shared data that is constantly pooled by all threads and spawn a game controller thread that controls the game.

More specifically, I would have a data structure Player that has some flag inGame. When sufficient players are available, the player that notices this starts the game control thread and passes the available players to it. The game control thread marks all available players as being "inGame" and assigns a question to them. After some time, it checks for an answer. In the meantime, the players constantly check for updates from the game control thread.

I feel that this shared data approach is not the best. Do you have any suggestion on how could I improve the architecture of my application?

1 Answer 1


If you want to block a number of threads until they are all ready, the common construct is a "barrier". You initialize it with a count, and once that many threads are waiting on the barrier, they are released.

If your library doesn't have a barrier, you can implement one using two mutexes and some logic, as described in this SO answer citing The Little Book of Semaphores.

  • Using a barrier is a nice idea to coordinate the threads. However, what's the best approach to handle question selection (the questions are in a database)?
    – Paul92
    Jan 9, 2016 at 14:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.