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