I was working on a small combat-code for fun trying to learn to use threads in c++ but stumpled upon a bug which i can't figure out.
I use a mutext for synchronisation:
mutex mux1;
and a threaded function:
void dueler(Player &player, Player &enemy)
{
     do
     {
          Sleep(player.get_as());
          mux1.lock();
          cout << player.get_name()<< " hit " << enemy.get_name() << " for " << player.get_str() << " damage!" << endl;
          enemy.dmg(player.get_str());
          mux1.unlock();
     } while ((enemy.ask_dead() != true) && (player.ask_dead() != true));
 }
which I call in main (full code: http://pastebin.com/1FBf2FCQ):
int main()
{
    Player Player1("kasper", 100, 5, 200);
    Player Player2("Bagger", 150, 8, 3000);
    thread dueler1(dueler,Player1,Player2);
    thread dueler2(dueler, Player2, Player1);
    dueler1.join();
    dueler2.join();
    cout <<endl<< "battle is over!";
}
The threaded function makes to Players (http://pastebin.com/ZCTfUYiS) fight against each other:
class Player
{
public:
    Player(string name_, int maxhp_, int strenght_, int attackspeed_) {
        name = name_; 
        maxhp = maxhp_; 
        hp = maxhp;
        strenght = strenght_;
        attackspeed = attackspeed_;
        isdead = false;
    }
    void fullhp() {  hp = maxhp;  }
    void dmg(int dmg) {
        hp -= dmg;
        if (hp < 0) dead();
    }
    void dead() {
        isdead = true;
        cout << name <<" died like a pessant";
    }
    string get_name() { return name; }
    int get_hp()    { return hp; }
    int get_maxhp() { return maxhp; }
    int get_str()   { return strenght; }
    int get_as()    { return attackspeed; }
    bool ask_dead() {return isdead; }
private:
    string name;
    int maxhp;
    int hp;
    int strenght;
    int attackspeed;
    bool isdead;
};
The problem happens when Player2 gets killed: 
- thread 1 ( which handles the damage dealt by player 1) stops as is should,
- thread 2 ( which handles Player2who's dead) continues to run, and completly ignores thewhilestatement until Player1 is dead.
I would have expected the while to end because of the condition (player.ask_dead() != true) should fail. 
Any suggestions?
 
     
    