task-detach-7.c 992 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. Each thread uses
  6. taskwait to wait for the child tasks to complete. */
  7. int main (void)
  8. {
  9. int x = 0, y = 0, z = 0;
  10. int thread_count;
  11. omp_event_handle_t detach_event1, detach_event2;
  12. #pragma omp parallel private (detach_event1, detach_event2)
  13. {
  14. #pragma omp single
  15. thread_count = omp_get_num_threads ();
  16. #pragma omp task detach (detach_event1) untied
  17. #pragma omp atomic update
  18. x++;
  19. #pragma omp task detach (detach_event2) untied
  20. {
  21. #pragma omp atomic update
  22. y++;
  23. omp_fulfill_event (detach_event1);
  24. }
  25. #pragma omp task untied
  26. {
  27. #pragma omp atomic update
  28. z++;
  29. omp_fulfill_event (detach_event2);
  30. }
  31. #pragma omp taskwait
  32. }
  33. assert (x == thread_count);
  34. assert (y == thread_count);
  35. assert (z == thread_count);
  36. }