So I have a simple code that behaves differently in 2 cases
In the first case, when I write it with (vecthread.size() - 5) inside the if statement it will go in the if statement and then throws an error "out_of_range". He goes in even tho the vector was empty.
vector<thread*> vecthread;
for (int i = 0; i < max; i++)
{
//int temp = vecthread.size() - 5;
if((vecthread.size() - 5) > 0) {
...
}
thread* newThread = new thread(forj, max, mkg, x, &counter, &countermax);
vecthread.push_back(newThread);
}
But if i use the temp int here like so:
vector<thread*> vecthread;
for (int i = 0; i < max; i++)
{
int temp = vecthread.size() - 5;
if(temp > 0) {
...
}
thread* newThread = new thread(forj, max, mkg, x, &counter, &countermax);
vecthread.push_back(newThread);
}
It will work correctly and not enter the if statement. Can anyone explain what's happening here? I don't wanna pollute my code with those unnessesary temp values if I can avoid it
Down below the full code:
void dspi() {
double pi = 2 * acos(0.0);
o.open("pi.dat");
int max = 100;
int max2 = pow(max,2);
vector<int> lfg {1};
vector<thread*> vecthread;
calculatorlfg(max, &lfg); //calculates pseudorandom numbers using the lagged Fibonacci-Generator method and writes them into lfg
int counter = 0;
int countermax = 0;
for (int i = 0; i < max; i++)
{
double x = betrag(lfg.at(i)/(pow(2,31)-1));
int bla = vecthread.size() - 5;
if((vecthread.size() - 5) > 0) {
vecthread.at(vecthread.size()-5)->join();
double temp = double(counter)/double(countermax);
o << counter << "\t" << countermax<< "\t" << temp << "\t" << temp - pi/4 << endl;
cout << countermax << "/" << max2 << endl;
}
thread* newThread = new thread(forj, max, mkg, x, &counter, &countermax);
vecthread.push_back(newThread);
}
o.close();
}