mirror of
https://github.com/OpenMP/Examples.git
synced 2025-04-07 23:22:11 +01:00
37 lines
679 B
C
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;
|
|
}
|