thread doesn't effect the while loop

hey, i finally got the thread working but still there something wrong with my program, because even though the thread works fine, my while loop continue to work after its argument don't meet with him any more.
why is that? and how i can fix that?

the while loop at line 56 when the turn() starts continue working after i press k, but the if statement in line 205 when turn() ends work just fine.
so eventually pressing k works just not immediately.
the thread is at the start of the turn () , and the function inside the thread is in the end of the program kkk() line 223.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#include <iostream>
#include "rabbbit.h"
#include <string>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <fstream>
#include <stdlib.h>
#include <conio.h>
#include <thread>
#include <chrono>
#include <windows.h>
#define BOUT(expr) cout << expr ; fout<< expr
using namespace std;

vector<rabbit> robit;
int l=0;
ofstream fout;
char theK;
bool ok= true;
int half;
void kkk();
void turn();

int o = 0;
int e;
int u=1;
int t=0;

int main()
{

    srand(time(0));
    fout.open("rabbit.txt");


    do
    {

        BOUT("new turn"<<endl<<endl);
        turn();
        Sleep(2000);


    }
    while(robit.size()>1);

    BOUT("GAME OVER!!!!");
    fout.close();
    return 0;

}

void turn()
{
    thread t1 (kkk);
    t1.detach();
    while(ok==true)
    {
        if(robit.size()<1)
        {

            robit.push_back(rabbit(rand(),1));
            t++;
            robit.push_back(rabbit(rand(),2));
            t++;
            robit.push_back(rabbit(rand(),3));
            t++;
            robit.push_back(rabbit(rand(),4));
            t++;
            robit.push_back(rabbit(rand(),5));
            t++;
        }

        for(o=0; o<robit.size(); o++)
        {
            if(robit[o].mutantRabbit==false)
            {
                robit[o].canBite=true;
            }
        }
        const int c = robit.size();
        for(o=0; o<c; o++ )
        {
            Sleep(1000);
            BOUT("rabbit "<< robit[o].name<< " turn"<< endl<<endl);
            if (robit[o].mutantRabbit==false && robit[o].canBite==true )
            {

                e = rand()%robit.size();

                for(l=0; l<robit.size();)
                {
                    if(robit[l].mutantRabbit==false)
                    {
                        l++;

                    }
                    else if(robit[l].mutantRabbit==true)
                    {
                        while(robit[e].mutantRabbit==false )
                        {
                            e = rand()%robit.size();
                        }

                        robit[e].mutantRabbit=false;
                        Sleep(1000);
                        BOUT("rabbit "<<robit[e].name<<" mutantRabbit is "<<robit[e].mutantRabbit<<" after he was bite from rabbit "<<robit[o].name<<endl<<endl<<endl);
                        l=robit.size();
                    }
                }
            }


            if(robit[o].mutantRabbit==true)
            {
                for(l=0; l<robit.size(); l++)
                {
                    if ( (robit[l].mutantRabbit==true) && (robit[l].age>1) &&(robit[l].sex==true)&&(robit[o].sex==false) && (robit[o].age>1))
                    {
                        Sleep(1000);
                        BOUT(endl<<"rabbit " <<robit[l].name<< " and rabbit "<<robit[o].name<<" made a baby rabbit."<<endl<<endl);
                        l=robit.size();
                        t++;
                        robit.push_back(rabbit(rand(),t));
                        robit[l].age = 0;
                        robit[l].color =  robit[o].color;

                    }
                }
            }
            if(robit.size()==1000)
            {
                for(l=0; l<500; l++)
                {
                    robit.erase(robit.begin()+rand()%(1000-l));
                }
                Sleep(1000);
                BOUT("there no food left 500 rabbits are dead"<<endl<<endl<<endl);
            }

            robit[o].ageing();
            if (robit[o].age>10 && robit[o].mutantRabbit==true)
            {
                Sleep(1000);
                BOUT("rabbit "<<robit[o].name<<" is dead."<<endl<<endl);
                robit.erase (robit.begin()+o);
                o--;
                u++;
            }
            else if (robit[o].age>50 && robit[o].mutantRabbit==false)
            {
                Sleep(1000);
                BOUT("rabbit "<<robit[o].name<<" is dead."<<endl<<endl);
                robit.erase (robit.begin()+o);
                o--;
                u++;
            }
            else
            {
                Sleep(1000);
                BOUT("rabbit "<<robit[o].name<<" as end is turn"<<endl<<endl);
            }

            if(o==c-u)
            {
                u=1;
                o=c;
            }

        }

        BOUT("there are now "<<robit.size()<<" rabbits in the garden"<<endl);
        if(robit.size()>0)
        {
            BOUT("the rabbits are:"<<endl);
        }
        Sleep(1000);
        for(l=0; l<51; l++)
        {
            for(o=0; o<robit.size(); o++)
            {
                if(robit[o].age==l)
                {
                    Sleep(1000);
                    BOUT(endl<<robit[o].name<<endl<<endl<<"age: "<<robit[o].age<<endl<<"color: "<<robit[o].color<<endl<<"sex: ");
                    if(robit[o].sex==true)
                    {
                        BOUT(robit[o].rabbitSex<<endl);
                    }
                    else
                    {
                        BOUT(robit[o].rabbitSex<<endl);
                    }
                    if(robit[o].mutantRabbit==false)
                    {
                        BOUT("mutant rabit"<<endl);
                    }
                    BOUT(endl<<endl);
                }

            }
        }

     if(ok==false)
        {

            BOUT("half of the rabbit are dead"<<endl);
            half = robit.size()/2;
            for(int k=0; k<half; k++ )
            {

                robit.erase (robit.begin() + rand()%robit.size());
            }
            BOUT("there are now "<< robit.size()<<" rabbits"<<endl);
            Sleep(1000);
            ok=true;
        }
    }
}


void kkk()
{
    cin>>theK;
    while(theK== 'k')
    {
        ok=false;
        theK='l';
        cin>>theK;
    }
    while(theK!='k')
    {
        cin>>theK;
    }
}




Last edited on
Would you mind posting "rabbit.h" and maybe "rabbit.c" so that we can compile\test this out?

You might try marking the 'ok' global variable as volitile: http://en.cppreference.com/w/cpp/language/cv
Optimizers can do weird things at times...
sure.

rabbit.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

#ifndef RABBIT_H
#define RABBIT_H
#include <string>
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;

class rabbit
{
    public:
        rabbit(int r,int p);

       void ageing();
      void go();
        virtual ~rabbit();
void info();
      bool mutantRabbit= true;
         bool canBite=false;

        bool sex;
        string rabbitSex;
        string color;
  string rabbitColor[4];
       int rabbitAge[51];
   int age;
        string name;
        string rabbitName[21];
        int volvol;
        int i;
        int b;
        int c;
        int q;



    private:

};


#endif // RABBIT_H





rabbit.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65


#include "rabbbit.h"
#include <iostream>
#include <ctime>
#include <string>
#include <cstdlib>
using namespace std;
rabbit::rabbit(int r,int p)
{

 sex;
     volvol = r%2;
 if (volvol>0){
     sex = true;
     rabbitSex= "male";
 }
 else{
        sex= false;
  rabbitSex = "female";
 }




  c = r%21;
   string rabbitName[21] = {"Steve", "Tom", "Rabi" , "Lorence", "Carrrot", "AssJucie","Nick","Israel","Noal","Hen","Keany","Lolo","Choclte","Nor","Lili","Kely","Nofar","Matilda","Igor","Joel","Nimrod",};
  name = rabbitName[c];

  int g = r%100;
    if (g<2){

            mutantRabbit = false;


    }

    if(p<6){
i = r%4;
   string rabbitColor[4]= {"white", "brown", "black", "spotted"};

 color = rabbitColor[i];
    }
   int rabbitAge[11];
for(int m =0;m<11;m++){
    rabbitAge[m]=m;
}
 if(p<6){ b = r%11;
 age = rabbitAge[b];

    }
}




void rabbit::ageing(){
    age++;

}

rabbit::~rabbit()
{

}




and i tried using volatile but it didn't work.
 
volatile bool ok= true;

any idea?
I haven't tested your code yet, but I see a lot of loops and a lot of sleeping. Is it possible that you just aren't waiting long enough for the control flow to reach the top of your while loop and test the variable again?
nope, i checked it now and this problem still exist even if i erase all the sleep in my program.
oh my god i understand my problem now, i thought that once inside the while loop the argument change from true to false the while loop stops.
but it appears that only when the while loop reach its end it checks its arguments.
so this is my screw up, and my question now is how i can make the if statement in line 205 active once i press 'k' and not just at the end of the while loop?
The easiest way would be to periodically check that variable and break when the condition is met.
no, actually i just put all the if statement at the kkk() and it did the job.
sorry for your trouble, and thank you for your help.
Topic archived. No new replies allowed.