In a C program using OpenMP, I want to set a flag when any thread (I don't need to know which one) meets a condition. If the flag variable is shared by all threads, and the flag is initialized to 0 (before the multi-thread part) and any thread will set the value to 1 or to 0 (all of them always to the same value), do I need a "#pragma omp atomic" directive?
For instance, the following code snippet:
//DataStruct is self defined data structure
function (DataStruct *data) {
int i,flag=0;
#pragma omp parallel for
for(i=0;i<data->maxval;i++) {
//Do stuff
if (/*check condition*/) {
//data->printMesage is 0 or 1, and doesn't change. It is fixed
//before calling this function
//data->printMesage is also an int variable
flag=data->printMesage;
}
}
//End of for loop. The code is running in
//single thread from here
if (flag) {
//Print message
}
}
It is necessary before the "flag=data->printMesage;" to add the "#pragma omp atomic" directive?