Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Threads permissions  (Read 520 times)

0 Members and 1 Guest are viewing this topic.


  • Newbie
  • *
  • Posts: 1
    • View Profile
    • Email
Threads permissions
« on: November 27, 2018, 12:58:04 am »
I'm trying to developp a game server able to at the same time :
- listen for new clients who may connect
- receive udp packets
- manage multiple games at the same time and process everything needeed for them to work

I made a class "Partie" which contains all my in-game processing fonctions, and multiple instances of "Partie" are declared in the main function. Each "Partie" contains an SFML thread (1) which is started when the game starts and contains all the processing functions of the game.
Another thread (2) is running to listen for incoming data packets and is supposed to process and call methods from "Partie" to interact with the main game engine.
And my problem is that when thread (2) calls a method from "Partie" which modifies variable from "Parties", it just crashes with a write access violation exeption. All the variables look fine, my guess is just that thread (2) can't for some reason access "Partie" variables...

If someone could explain me how premissions work with threads, it would be amazing !

My class "Partie" :

class Partie {


                : localGameEngine(&Partie::gameEngineTheServerSide, this)

        void setStartStatus(bool status);
        void setClientsIp(IpAddress ip, int internalClientId);
        void setClientsPort(unsigned short port, int internalClientId);
        void setAndSendAseroid(Asteroid asteroidToProcess, IpAddress sourceIP, unsigned short sourcePort, int id);
        void sendMissile(Missile missileToProcess, IpAddress sourceIP, unsigned short sourcePort);
        void sendSpaceShip(Spaceship spaceshipToSend, IpAddress sourceIP, unsigned short sourcePort, int id);
        IpAddress getClientIp(int clientId);
        bool getGameStatus();
        void setClientStatus(Client client, int id);
        void startGame();

        int deadSpaceShips = 0; //évidemment à changer dès que chaque shapceship aura un numéro clairement implémenté et que donc je pourrais les identifier chacun et vérifier si ils sont morts ou non

        Thread localGameEngine;
        Client clients[MAX_CLIENTS_PER_GAME];
        bool isStarted;

        Asteroid asteroids[ASTEROIDS_TABLE_LENGTH];

        void gameEngineTheServerSide();
        void asteroidsNextFrame(Asteroid *asteroids);
        Asteroid generateAsteroid(const int typeProbabilities[NB_TYPES]);
        void noAsteroidExist(Asteroid *asteroids);
        void initMissiles(Missile *missiles);
        void initSpaceship(int id, Spaceship &spaceship);
        bool asteroidManager(/*Asteroid *asteroids,*/ int &frames, float &secsInterval, bool &initGame, bool &reduce);

The code running on thread (2) :

if (dataType == DataType::Asteroid) {

                Asteroid asteroid;

                Uint32 nb;
                packet >> nb;
                int id = nb;

                cout << "Asteroid " << id << " has been destroyed by " << originIp << endl ;

                packet >> asteroid.speed >> asteroid.angle >> asteroid.angularVelocity >>
                        asteroid.exists >> asteroid.hasEnteredWindow;

                // ConvexShape

                Uint32 type;
                packet >> type;
                asteroid.type = type;

                Uint32 count;
                packet >> count;

                ConvexShape convexShape;

                // Points
                /// Float
                float x, y;
                for (int i = 0; i < count; i++) {
                        packet >> x >> y;
                        convexShape.setPoint(i, Vector2f(x, y));

                // Color
                /// R, G, B = Uint8
                Uint8 r, g, b;
                packet >> r >> g >> b;
                convexShape.setFillColor(Color::Color(r, g, b));

                // Position
                /// Float
                packet >> x >> y;
                convexShape.setPosition(x, y);

                asteroid.shape = convexShape;

                games[getGameID(originIp, originPort, waitingClients)].setAndSendAseroid(asteroid, originIp, originPort, id); //here is the method called

And here is the code of the method :

void Partie::setAndSendAseroid(Asteroid asteroidToProcess, IpAddress sourceIP, unsigned short sourcePort, int id)
        asteroids[id] = asteroidToProcess; //I get the write access violation exception here
        sendData(asteroidToProcess, id, sourceIP, sourcePort, true, clients);

I'm probably just stupid and doing something completely wrong,  but if someone could at least explain me what am i doing wrong, it would be amazing.

Sorry for my somehow bad english
And feel free to ask me anything i could have forget to explain and you may need in order to help me, obviously
« Last Edit: November 27, 2018, 07:50:59 am by Laurent »


  • Administrator
  • Hero Member
  • *****
  • Posts: 32060
    • View Profile
    • SFML's website
    • Email
Re: Threads permissions
« Reply #1 on: November 27, 2018, 07:54:54 am »
First, please avoid the XY problem. You have a crash, so that's what you should describe; don't get people lost with "thread permissions" (such a thing doesn't even exist). Don't make random guesses, just describe what you have with as many relevant details as possible.

Then, when developers experience crashes, what they usually do is to run a debugger, which is much more helpful to diagnose runtime errors than people on a forum ;)
Laurent Gomila - SFML developer