Shared variables in OpenMP

Viewed 7759

I have a very basic question (maybe stupid) regarding shared variables in OpenMP. Consider the following code:

void main()
{
int numthreads;
#pragma omp parallel default(none) shared(numthreads)
 {
  numthreads = omp_get_num_threads();
  printf("%d\n",numthreads);
 }
}

Now the value of numthreads is the same for all threads. is there a possibility that since various threads are writing the same value to the same variable, the value might get garbled/mangled ? Or is this operation on a primitive datatype guaranteed to be atomic ?

1 Answers
Related