I came up with an interesting situation. I have a bool variable and then I want multiple threads to perform its own independent tasks and then mark that bool depending on the thread's result. This variable is never read by any of the threads and is never used before all writing tests have finished. Like this:
public bool F(){
bool flag = false;
Parallel.ForEach(list, element =>
{
bool result = // Do independent work here;
if (result) flag = true;
});
return flag;
}
Notice how I never read flag inside my Parallel.ForEach. But what could happen is having multiple threads attempting to write true to flag (but never false). Is it safe to do this?