summaryrefslogtreecommitdiff
path: root/libgomp/testsuite/libgomp.c/appendix-a/a.39.1.c
blob: a129e87fc49f12b0000ecb69ab3126c87b2117cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/* { dg-do run } */

#include <stdio.h>
#include <omp.h>
void
skip (int i)
{
}

void
work (int i)
{
}
int
main ()
{
  omp_lock_t lck;
  int id;
  omp_init_lock (&lck);
#pragma omp parallel shared(lck) private(id)
  {
    id = omp_get_thread_num ();
    omp_set_lock (&lck);
    /* only one thread at a time can execute this printf */
    printf ("My thread id is %d.\n", id);
    omp_unset_lock (&lck);
    while (!omp_test_lock (&lck))
      {
	skip (id);		/* we do not yet have the lock,
				   so we must do something else */
      }
    work (id);			/* we now have the lock
				   and can do the work */
    omp_unset_lock (&lck);
  }
  omp_destroy_lock (&lck);
  return 0;
}