Suppose we have the following code schema:
main(){
sometype data;
#pragma omp parallel for
for(i=0; i< n; i++){
read data;
do some calculations that would be used in order to update the data;
#pragma omp critical{
update data;
}
}
}
I know that we need a critical section for updating the data because two updates at the same time could lead to corruption, however what can happen if a thread is inside the critical section updating the data and another thread is trying to read the data?
I've seen some examples online where reading is considered a safe action in a multi threading system, however I'm not sure how safe it can be in the above situation. If it's not safe, what would be the appropriate action in order to make it safe?
thank you in advance