OpenMP-Examples/sources/Example_mem_model.1c.c
2015-01-13 11:38:24 -08:00

37 lines
679 B
C

/*
* @@name: mem_model.1c
* @@type: C
* @@compilable: yes
* @@linkable: yes
* @@expect: rt-error
*/
#include <stdio.h>
#include <omp.h>
int main(){
int x;
x = 2;
#pragma omp parallel num_threads(2) shared(x)
{
if (omp_get_thread_num() == 0) {
x = 5;
} else {
/* Print 1: the following read of x has a race */
printf("1: Thread# %d: x = %d\n", omp_get_thread_num(),x );
}
#pragma omp barrier
if (omp_get_thread_num() == 0) {
/* Print 2 */
printf("2: Thread# %d: x = %d\n", omp_get_thread_num(),x );
} else {
/* Print 3 */
printf("3: Thread# %d: x = %d\n", omp_get_thread_num(),x );
}
}
return 0;
}