forked from sudikrt/assignments
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcondition.c
67 lines (49 loc) · 1.44 KB
/
condition.c
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#define COUNT_1 3
#define COUNT_2 6
#define COUNT_END 10
int count = 0;
pthread_mutex_t c_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t c_var = PTHREAD_COND_INITIALIZER;
void* funct1 ()
{
for(;;)
{
pthread_mutex_lock (&c_mutex);
pthread_cond_wait (&c_var, &c_mutex);
count++;
printf ("Cvalue Funct1 %d:\n",count);
pthread_mutex_unlock (&c_mutex);
if(count >= COUNT_END) return(NULL);
}
}
void* funct2 ()
{
for(;;)
{
pthread_mutex_lock (&c_mutex);
if( count < COUNT_1 || count > COUNT_2)
{
pthread_cond_signal ( &c_var);
}
else
{
count++;
printf ("Cvalue Funct2 %d:\n", count);
}
pthread_mutex_unlock (&c_mutex);
if(count >= COUNT_END) return(NULL);
}
}
int main ()
{
pthread_t thread_1, thread_2;
pthread_create (&thread_1, NULL, &funct1, NULL);
pthread_create (&thread_2, NULL, &funct2, NULL);
pthread_join (thread_1, NULL);
pthread_join (thread_2, NULL);
return 0;
}