Thursday, 15 April 2010

c++ - Threads doesn't end correctly: it ignores failing loop conditions -


I am trying to learn to use threads in C ++, but to be stumped on a bug I'm not working for fun.

I use a mutetek for synchronization:

  mutex mux1;  

and a threaded function:

  zero dualer (player and player, player and enemy) {(sleep (player .get_as ()); mux1 .lock (); Cout & lt; player.get_name () & lt; "hit" & lt; enemy ;get_name () & lt; "for" & lt; & Lt; player .get_tr () << "damage!" & Lt; endl; enemy.dmg (player.get_str ()); mux1.unlock ();} while ((enemy.ask_dead ()! = True) & amp; amp; amp; (player.ask_dead ()! = True));}  

Which I call in main (full code):

  int main (player player 1 ("Casper", 100, 5, 200); Player Player 2 ("Bigger", 150, 8, 3000); Thread Deller 1 (Dialer, Player 1, Player 2); Thread Dweller 2 (Dialer, Player 2, Player 1); dueler1.join (); dueler2.join (); Cout & lt; endl & lt; Lt; "war is over!";}  

makes threaded function player () fight against each other:

  class player {Public: Player (string name_, NTMHP_, int strangut_, int objection:) {name = name_; Maxhp = maxhp_; Hp = maxhp; Strength = sharp; Attacks / attacks; Ided = false; } Zero is complete () {hp = maxhp; } Zero dmg (int dmg) {hp - = dmg; If (HP & lt; 0) dead (); } Zero free () {isdead = true; Cout & lt; & Lt; Name & lt; & Lt; "Died like a pitiable"; } String get_name () {return name; } Int get_hp () {return hp; } Int get_maxhp () {return maxhp; } Int get_str () {Return Strength; } Int get_as () {returnpepeed; } Bool ask_dead () {return isdead; } Private: string name; Intestine maxhp; Intel HP; Intestine; Int attacking; Bull-eyed; };  

The problem occurs when Player2 becomes:

  • Thread 1 (which handles the damage dealt by player 1
  • Thread 2 (which is the Player2 that dies) continues to run, and Player1 is dead until while details Completely ignore

I was expecting to end because of condition (player.ask_dead (!)! = True) should fail .

Any suggestions?

You have properly synchronized by putting a mutete to avoid concurrent access to your object. It's already a very good start!

Problem:

The problem is that when you make a thread, it creates a personal copy of the arguments passed by you (see also)

You can easily verify the references by adding these first statements to the duler ():

  cout & lt; & Lt; "Dealer with player" & lt; & Lt; (Zero *) and player & lt; & Lt; "And Anonymous" & lt; & Lt; (Zero *) and Enemy & lt; & Lt; Endl;  

When called dueler () based on context, the reference does not refer to the original object (local variables in main)> ) But on its clone,

solution:

You have to use to make sure that all the threads actually see the same object:

  Thread dueler1 (Duel, Std :: ref (Player1), std :: ref (Player2)); Thread Deller 2 (Dualer, Stade :: Ref (Player 2), std :: Ref (Player 1));  

Other comments:

When a player dies, then its Dual Answer () function is most waiting for Mute X is. So when the mitix is ​​released, it damages despite being dead

OK! In one game you can argue that the player is dead but still is walking with the person suffering from this sword ... but we can fix it by adding a condition when entering the safe area:

  mux1.lock (); // We were waiting, at a certain time (if! Players. Ask_dead ()) {// then check whether we are still alive & lt; & Lt; Player .get_name () & lt; & Lt; "Hit" & lt; & Lt; Enemy .get_name () & lt; & Lt; "For" & lt; & Lt; Player .get_st () & lt; & Lt; " Damage!" & Lt; & Lt; Endl; Enemy.dmg (player.get_str ()); } Mux1.unlock ();  


No comments:

Post a Comment