-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththread_con.c
74 lines (63 loc) · 1.79 KB
/
thread_con.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
68
69
70
71
72
73
74
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
pthread_mutex_t count_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t condition_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t condition_cond = PTHREAD_COND_INITIALIZER;
void *functionCount1();
void *functionCount2();
int count = 0;
#define COUNT_DONE 10
#define COUNT_HALT1 3
#define COUNT_HALT2 6
main()
{
pthread_t thread1, thread2;
pthread_create( &thread1, NULL, &functionCount1, NULL);
pthread_create( &thread2, NULL, &functionCount2, NULL);
pthread_join( thread1, NULL);
pthread_join( thread2, NULL);
exit(0);
}
void *functionCount1()
{
for(;;)
{
pthread_mutex_lock( &condition_mutex );
printf("thread 1 got the lock \n");
while( count >= 3 && count <= 6 )
{
printf("waiting here \n");
pthread_cond_wait( &condition_cond, &condition_mutex );
}
pthread_mutex_unlock( &condition_mutex );
printf("thread 1 unlocked \n");
pthread_mutex_lock( &count_mutex );
printf("thread 1 locked count mutex \n");
count++;
printf("Counter value functionCount1: %d\n",count);
pthread_mutex_unlock( &count_mutex );
if(count >= 10) return(NULL);
}
}
void *functionCount2()
{
for(;;)
{
pthread_mutex_lock( &condition_mutex );
printf("thread 2 got the lock \n");
if( count < 3 || count > 6 )
{
printf("signal at count %d \n",count);
pthread_cond_signal( &condition_cond );
}
pthread_mutex_unlock( &condition_mutex );
printf("thread 2 unlocked \n");
pthread_mutex_lock( &count_mutex );
printf("thread 2 locked count mutex \n");
count++;
printf("Counter value functionCount2: %d\n",count);
pthread_mutex_unlock( &count_mutex );
if(count >= 10 return(NULL);
}
}