Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>When using concurrency, it is always difficult to give good responses. It highly depends of what your are really doing and what really matter.</p> <p>From my understanding, a player move involve :</p> <p>1 Updading player position.</p> <p>2 Removing the player from previous field.</p> <p>3 Adding player to new field.</p> <p>Imagine you use several locks at the same time but aquire only one at a time : - Another player can perfectly look at the wrong moment, basically between 1&amp;2 or 2&amp;3. Some player can appear to have disapeared from the board for exemple.</p> <p>Imagine your do imbricked locking like this :</p> <pre><code>synchronized(player) { synchronized(previousField) { synchronized(nextField) { ... } } } </code></pre> <p>The problem is... It don't work, see this order of execution for 2 threads :</p> <pre><code>Thread1 : Lock player1 Lock previousField Thread2 : Lock nextField and see that player1 is not in nextField. Try to lock previousField and so way for Thread1 to release it. Thread1 : Lock nextField Remove player1 from previous field and add it to next field. Release all locks Thread 2 : Aquire Lock on previous field and read it : </code></pre> <p><strong>Thread 2 think that player1 as disapeared from the whole board.</strong> If this is a problem for your application, you can't use this solution.</p> <p>Additionnal problem for imbriqued locking : threads can become stuck. Imagine 2 players : they exchange their position at exactly the same time :</p> <pre><code>player1 aquire it's own position at the same time player2 aquire it's own position at the same time player1 try to acquire player2 position : wait for lock on player2 position. player2 try to acquire player1 position : wait for lock on player1 position. </code></pre> <p>=> Both players are blocked.</p> <p>Best solution in my opinion is to use only one lock, for the whole state of the game.</p> <p>When a player want to read the state, it lock the whole game state (players &amp; board), and make a copy for its own usage. It can then process without any lock.</p> <p>When a player want to write the state, it lock the whole game state, write the new state and then release the lock.</p> <p>=> Lock is limited to read/write operations of the game state. Player can perform "long" examination of the board state on their own copy.</p> <p>This prevent any inconsistant state, like a player in several field or none, but don't prevent that player can use "old" state.</p> <p>It can appear weird, but it is the typical case of a chess game. When you are waiting for the other player to move, you see the board as before the move. You don't know what move the other player will make and until he has finally moved, you work on an "old" state.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload