Consider :
int [] ii;
foreach(i,dummy; parallel(somearray)) {
ii ~= somefunc(dummy);
}
This is not safe, because all threads are accessing the same array and trying to add values and leading to collision.
But :
int [] ii;
ii.length = somearray.length;
foreach(i,dummy; parallel(somearray)) {
ii[i] ~= somefunc(dummy);
}
This is safe. In this case, threads are accessing an unique memory location each.
But what about this :
int [ string ] ii;
ii.length = somearray.length;
foreach(i,dummy; parallel(somearray)) {
string j = generateUniqueString(i);
ii[j] ~= somefunc(dummy);
}
Is this also guaranteed thread safe?
In my 5 runs, I did not see any problems, but I'd like to confirm. Thank you.