testsuite/libgomp.c/appendix-a/a.39.1.c

Go to the documentation of this file.
00001 /* { dg-do run } */
00002 
00003 #include <stdio.h>
00004 #include <omp.h>
00005 void
00006 skip (int i)
00007 {
00008 }
00009 
00010 void
00011 work (int i)
00012 {
00013 }
00014 int
00015 main ()
00016 {
00017   omp_lock_t lck;
00018   int id;
00019   omp_init_lock (&lck);
00020 #pragma omp parallel shared(lck) private(id)
00021   {
00022     id = omp_get_thread_num ();
00023     omp_set_lock (&lck);
00024     /* only one thread at a time can execute this printf */
00025     printf ("My thread id is %d.\n", id);
00026     omp_unset_lock (&lck);
00027     while (!omp_test_lock (&lck))
00028       {
00029     skip (id);      /* we do not yet have the lock,
00030                    so we must do something else */
00031       }
00032     work (id);          /* we now have the lock
00033                    and can do the work */
00034     omp_unset_lock (&lck);
00035   }
00036   omp_destroy_lock (&lck);
00037   return 0;
00038 }

Generated on Fri Apr 5 05:38:10 2013 for Libgomp by  doxygen 1.4.7