-
Notifications
You must be signed in to change notification settings - Fork 0
/
cond_multithread.c
executable file
·80 lines (70 loc) · 2.03 KB
/
cond_multithread.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
75
76
77
78
79
80
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
pthread_cond_t full = PTHREAD_COND_INITIALIZER, empty = PTHREAD_COND_INITIALIZER;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
int buffer[4];
int begin = 0, end = 0, counter = 0, max = 4;
void *producer( void *arg ){
printf("pthread producer id : %d run \n", *(int *) arg );
srand(1);
while( 1 )
{
pthread_mutex_lock( &mutex );//一定要加锁
while( counter == max )
pthread_cond_wait( &full, &mutex );
buffer[ end ] = rand()%10;
counter++;
printf("pthread producer id :%d produce at %d and counter is %d\n", *(int *) arg, buffer[ end ], counter );
end = ++end % max;
pthread_mutex_unlock( &mutex );
pthread_cond_signal( &empty );
}
return NULL;
}
void *consumer( void *arg )
{
printf("pthread consumer id : %d run \n", *(int *)arg );
while( 1 )
{
pthread_mutex_lock( &mutex );//一定要先加锁
while( counter == 0 )
pthread_cond_wait( &empty, &mutex );
counter--;
printf("pthread consumer id :%d consumer at %d and counter is %d\n", *(int *) arg, buffer[ begin ], counter );
begin = ++begin % max;
pthread_mutex_unlock( &mutex );
pthread_cond_signal( &full );
}
return NULL;
}
int main(int argc, char const *argv[]) {
pthread_t pthread_consumer[3];
pthread_t pthread_producer[2];
int index = 0, *p = NULL ;
for( index = 0; index < 3; index++ )
{
p = (int *)malloc( sizeof(int) );
*p = index;
if( pthread_create( &pthread_consumer[ index ], NULL, producer, p ) != 0)
{
printf("create consumer error\n");
return -1;
}
}
for( index = 0; index < 2; index++ )
{
p = (int *)malloc( sizeof(int) );
*p = index;
if( pthread_create( &pthread_producer[ index ], NULL, consumer, p ) != 0)
{
printf("create consumer error\n");
return -1;
}
}
for( index = 0; index < 3; index++ )
pthread_join( pthread_consumer[ index ], NULL );
for( index = 0; index < 2; index++ )
pthread_join( pthread_producer[ index ], NULL );
return 0;
}