critical-2.c 618 B

123456789101112131415161718192021222324252627282930313233343536
  1. // { dg-do run }
  2. // Test several constructs within a parallel. At one point in development,
  3. // the critical directive clobbered the shared clause of the parallel.
  4. #include <omp.h>
  5. #include <stdlib.h>
  6. #define N 2000
  7. int main()
  8. {
  9. int A[N];
  10. int nthreads;
  11. int i;
  12. #pragma omp parallel shared (A, nthreads)
  13. {
  14. #pragma omp master
  15. nthreads = omp_get_num_threads ();
  16. #pragma omp for
  17. for (i = 0; i < N; i++)
  18. A[i] = 0;
  19. #pragma omp critical
  20. for (i = 0; i < N; i++)
  21. A[i] += 1;
  22. }
  23. for (i = 0; i < N; i++)
  24. if (A[i] != nthreads)
  25. abort ();
  26. return 0;
  27. }