a.39.1.c 680 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. /* { dg-do run } */
  2. #include <stdio.h>
  3. #include <omp.h>
  4. void
  5. skip (int i)
  6. {
  7. }
  8. void
  9. work (int i)
  10. {
  11. }
  12. int
  13. main ()
  14. {
  15. omp_lock_t lck;
  16. int id;
  17. omp_init_lock (&lck);
  18. #pragma omp parallel shared(lck) private(id)
  19. {
  20. id = omp_get_thread_num ();
  21. omp_set_lock (&lck);
  22. /* only one thread at a time can execute this printf */
  23. printf ("My thread id is %d.\n", id);
  24. omp_unset_lock (&lck);
  25. while (!omp_test_lock (&lck))
  26. {
  27. skip (id); /* we do not yet have the lock,
  28. so we must do something else */
  29. }
  30. work (id); /* we now have the lock
  31. and can do the work */
  32. omp_unset_lock (&lck);
  33. }
  34. omp_destroy_lock (&lck);
  35. return 0;
  36. }