task-detach-5.c 895 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /* { dg-do run } */
  2. #include <omp.h>
  3. #include <assert.h>
  4. /* Test tasks with detach clause. Each thread spawns off a chain of tasks,
  5. that can then be executed by any available thread. */
  6. int main (void)
  7. {
  8. int x = 0, y = 0, z = 0;
  9. int thread_count;
  10. omp_event_handle_t detach_event1, detach_event2;
  11. #pragma omp parallel private (detach_event1, detach_event2)
  12. {
  13. #pragma omp single
  14. thread_count = omp_get_num_threads ();
  15. #pragma omp task detach (detach_event1) untied
  16. #pragma omp atomic update
  17. x++;
  18. #pragma omp task detach (detach_event2) untied
  19. {
  20. #pragma omp atomic update
  21. y++;
  22. omp_fulfill_event (detach_event1);
  23. }
  24. #pragma omp task untied
  25. {
  26. #pragma omp atomic update
  27. z++;
  28. omp_fulfill_event (detach_event2);
  29. }
  30. }
  31. assert (x == thread_count);
  32. assert (y == thread_count);
  33. assert (z == thread_count);
  34. }