-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathptest.c
65 lines (55 loc) · 1.12 KB
/
ptest.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
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
//for thread test
#define BUFF_SIZE 8
//int wptr = 0 ;
//int rptr = 0 ;
int usedw = 0;
/*
void cycle_cnt(int* cnt,int value) {
*cnt = *cnt+1 ;
if( *cnt >= value )
*cnt=0;
}
*/
void* producer() {
while(1) {
if( usedw <= BUFF_SIZE-1 ) {
sleep(2) ;
usedw = usedw + 1 ;
printf("producer with %d usedw\n",usedw) ;
}
else
sleep(1) ;
}
return NULL ;
}
void* consumer() {
while(1) {
if( usedw >= 2+1 ) { //at least two
sleep(5) ;
usedw = usedw - 2 ;
printf("\tconsumer with %d usedw\n",usedw) ;
} else
sleep(1) ;
}
return NULL ;
}
int main() {
pthread_t pro,con ;
int ret1,ret2 ;
printf("creating 2 threads\n") ;
ret1 = pthread_create(&pro,NULL,producer,NULL) ;
ret2 = pthread_create(&con,NULL,consumer,NULL) ;
if( (ret1!=0) || (ret2!=0) ) {
printf("thread creation failure\n") ;
exit(EXIT_FAILURE) ;
} else {
printf("thread creation success\n") ;
}
pthread_join(pro, NULL);
pthread_join(con, NULL);
printf("main thread exit\n") ;
exit(EXIT_SUCCESS) ;
}