1 #include <errno.h>
2 #include <math.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <pthread.h>
7 #include <unistd.h>
8
9 pthread_mutex_t mutex;
10 pthread_cond_t cond;
11 int x;
12 int y;
13
14
15 /*
16 * === FUNCTION ======================================================================
17 * Name: x_lt_y
18 * Description:
19 * =====================================================================================
20 */
21 void *
22 x_lt_y ( void *arg )
23 {
24 printf ( "FUNCTION %s: muetx lock\n" , __FUNCTION__ );
25 pthread_mutex_lock(&mutex);
26 if ( x <= y ) {
27 printf ( "FUNCTION %s: x is %d, y is %d\n", __FUNCTION__, x, y );
28 printf ( "FUNCTION %s: before cond_wait\n" , __FUNCTION__);
29 pthread_cond_wait( &cond, &mutex);
30 printf ( "FUNCTION %s: after cond_wait\n" , __FUNCTION__);
31 }
32 printf ( "FUNCTION %s: muetx unlock\n" , __FUNCTION__);
33 pthread_mutex_unlock(&mutex);
34 return NULL;
35 } /* ----- end of function decrement ----- */
36
37 /*
38 * === FUNCTION ======================================================================
39 * Name: x_gt_y
40 * Description:
41 * =====================================================================================
42 */
43 void *
44 x_gt_y ( void *arg )
45 {
46 printf ( "FUNCTION %s: muetx lock\n" , __FUNCTION__);
47 pthread_mutex_lock(&mutex);
48 x++;
49 y--;
50 if ( x > y ) {
51 printf ( "FUNCTION %s: x is %d, y is %d\n", __FUNCTION__, x, y );
52 printf ( "FUNCTION %s: before cond_signal\n" , __FUNCTION__);
53 pthread_cond_signal( &cond );
54 printf ( "FUNCTION %s: after cond_signal\n" , __FUNCTION__);
55 }
56 printf ( "FUNCTION %s: muetx unlock\n" , __FUNCTION__);
57 pthread_mutex_unlock(&mutex);
58 return NULL;
59 } /* ----- end of function increment_count ----- */
60
61 /*
62 * === FUNCTION ======================================================================
63 * Name: main
64 * Description: main function
65 * =====================================================================================
66 */
67 int
68 main ( int argc, char *argv[] )
69 {
70 printf ("\nProgram %s\n\n", argv[0] );
71
72 pthread_t tid1;
73 pthread_t tid2;
74
75 pthread_mutex_init( &mutex, NULL );
76 pthread_cond_init( &cond, NULL );
77 x = 1;
78 y = 2;
79
80 pthread_create( &tid1, NULL, x_lt_y, NULL );
81 sleep(2);
82
83 pthread_create( &tid2, NULL, x_gt_y, NULL );
84 sleep(2);
85
86 return EXIT_SUCCESS;
87 } /* ---------- end of function main ---------- */