Hello guys! I'm programming with a friend a basic 2d videogame with C++ and SFML.
We're using a very basic main-thread in the server that updates every entity etc., and one thread per client that handles the communication with tcpsockets.
However, when I tried to program a multiplayer game in the past, I encountered a common problem with multithreading: my client thread tried to read a main thread variable (like a coordinate) while it was modifying it/deleting it, resulting in a SIGSEGV. In the past, I've resolved this using a list of game snapshots and atomic variables, every time the main thread updated the game it was pushed in a game_snapshot list, creating a list of "game states", the client had just to take the head of the list, convert and send it, while the main thread deleted all the object behind the 100th node (only if one client-thread was not using it).
Is that method correct? We're not currently right now thinking to change the "communication method" (like changing the main thread and one thread per client for something more efficient like async multiplexing), since this is a very basic project done to learn game dev in c++, but the "snapshot method" seems pretty inefficient to me. 100 instances of the same game seems pretty heavy to me.
P.s. No, mutex is not a solution. If a client lags and blocks the access to the game, the game freezes for every one. Same if the client has, for example, 1000+ clients, with every thread trying to mute the variables of the main thread. We're obviously never getting to have more han 4 clients, but this project has been created to learn the basics.
Thanks you so much! Sorry for any spelling mistake, I'm not really good in English :-(