To execute a loop in parallel I am using:
int testValues[16]={5,2,2,10,4,4,2,100,5,2,4,3,29,4,1,52};
parallel_for (1, 100, 1, [&](int i){
int var1;
for (var1=1; var1<=20000; var1++) {
int var2, var3, var4;
double u[45],pl;
int values[16]={-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};
/* ... */
for (var4=0; var4<16; var4++) {
if (values[var4] != testValues[var4]) break;
}
/* ... */
}
}
Can I be sure that all variables defined inside the parallel_for block (i.e. var1, var2, var3, var4, u, pl, values) will have scope local to each loop iteration, i.e. will not be shared across threads or loop iterations?
Also, is it safe for multiple threads to access testValues like this as long as they are only reading (not writing to it)?
All variables inside the lambda are local to the thread which executes it, so yes they are safe.
Reading/writing from/to the testValues array is theoretically a racecondition but if you are making sure that you are only reading and not writing at the same time or only write to different parts of the array it should be safe.