I have a timing issue and I don’t know how to fix it.
In my second loop, Display card, it loops and each connection starts a class called cLookAtCardConnection. This class will sometimes set the flag mPlayerList.GetNextFlag(), thus causing the loop to exit and the program goes to the first loop, Turn card over, which calls the class cLookAtCardConnection
The problem is that after mPlayerList.GetNextFlag() is set, the next couple connections are still in the same loop and call the class cLookAtCardConnection instead of (exiting?) the loop when the flag is set and going into the loop that calls cLookAtCardConnection.
Why is there a delay in the loop exiting after the flag has been set?
while( lBoard.Next()==true)
{
mPlayerList.Next();
// inner loop wait for all players
/////////////////////////////////////////////////////////////////////////////
// turn one card over
mPlayerList.WaitForAllPlayers();
do
{
do{
r=GetClient();
switch(r)
{
case 0: return; // exitvon a very bad error
}
} while(r==2);// loop if it was a timeout
cTurnCardOvrerConnection thread = new cLookAtCardConnection("thread3", connection, mPlayerList, mPlayersMessages, lBoard);
} while( mPlayerList.AllPlayersFinished()==false);// end while
//////////////////////////////////////////////////////////////////////////
// Display card -LOOK AT CARD
mPlayerList.ClearNextFlag();
mPlayerList.WaitForAllPlayers();
do
{
System.out.println(Thread.currentThread()+": Display card \r");
do{
r=GetClient();
switch(r)
{
case 0: return; // exitvon a very bad error
}
} while(r==2);// loop if it was a timeout
cLookAtCardConnection thread = new cLookAtCardConnection("thread3", connection, mPlayerList, mPlayersMessages, lBoard);
// after this flag is set the next couple connectons are still in this loop???
} while( mPlayerList.GetNextFlag()==false);// end while
} // reloop game board
} // loop forever
// System.out.println("--------- Eit player loop ------------------- \r");
} catch(IOException ec)
{
System.out.println(ec.getMessage());
}
} // end run
} // end of class
You probably need to set up a
synchronizedblock so that the code inside thedo-whileloop is only accessible to a single thread at a time.