Forum Multiplayer Locking again

Discussion and help relating to the PlayerIO Multiplayer API.

Locking again

Postby garysimmons » September 18th, 2011, 1:29 pm

Hi Guys,

I'm still pretty new to all this locking of threads etc and just wanted to ask for some advice.

In my code I use a dictionary to store a hash table for my players and their associated objects from the database. This hash table is populated with all users that are either in this room or were in this room last time they player. For example, there might be 5 people actually connected and flying their spaceships in a solar system but there maybe 10 other ships there belonging to other players that are not currently connected.

So..when my game started the players spaceships and player objects are stored (via an intermediate structure) into a dictionary. The intermediate structure also contains an additional Player reference for those players that are currently connected.

When a new player joins the game room is is put into a Connection Queue (just a list) which is processed in the next update. This processing involves nothing more than fetching the players corresponding structure from the hashtable/dictionary and store the player object in the structure stored there. Therefore, I know that any object in the has that has its Player member populated is currently online while those that are set to null are not currently online (but still must be rendered and receive battle damage if fired upon etc)

So my question is where I need to lock (my lock object) during the processing of the connection queue. As you can see, I need to iterate through the connection queue so ideally I would like to lock at this point so that while this is being processed (UserJoined) can't add more things (or UserLeft) remove things from that queue. However..in that loop through the objects I also do the database reads (if there ship and player objects how not yet been loaded) and thus my confusion comes in the handler for the database call.

Code: Select all

// SHOULD I LOCK HERE
for (int i = ConnectionQueue.Count-1; i >= 0; i--) {
       // Get the player
        Player p = ConnectionQueue[i];

       // If User does not yet exist in hash table then create and add it
       // This will happen when user joins after game world has been created
       if (!Users.ContainsKey(p.ConnectUserId))  {

             // We need to fetch the player and ship objects from the database
              PlayerIO.BigDB.Load("PlayerObjects", p.ConnectUserId,
                            delegate(DatabaseObject dbo) {

                                // If its loaded correctly
                                if (dbo != null)
                                {
                                      // If a user is already in queue (loading the ship might have done that already)
                                       lock (_LockObject)
                                      {
                                            REMOVE OR ADD TO MY HASHTABLE HERE
                                      }
                                 }
                             }
             }



Inside the database handler I want to lock my lock object prior to altering my hashtable/dictionary as you can see in the above code. However...the function that called the handler (via the BigDB operation) also wants to lock while accessing the connection queue.

My misunderstanding is how these locks work in the case of the asynchronous database handler. Is it ok to lock in both these places or could this cause some sort of dead lock. For example....Imagine the handler is executed before the parent function has finished processing. The handler would then sit and wait for the lock. If there were a hundred people in the queue (just an example) would I have 100 handler threads waiting for the lock to be released by the parent function and would this be too many for the service to handle.

Any advice would be greatly appreciated and sorry for the rather noob questions....c# and muti threading are all totally new to me
garysimmons
 
Posts: 99
Joined: May 15th, 2011, 12:02 pm

Return to Multiplayer



cron