-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththread.c
More file actions
54 lines (34 loc) · 1001 Bytes
/
thread.c
File metadata and controls
54 lines (34 loc) · 1001 Bytes
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
#include <stdio.h>
#include <pthread.h>
pthread_mutex_t mute;
int value = 0;
#define NUM_MAX 100*10000
void *productor(void *arg){
while(value < NUM_MAX) {
pthread_mutex_lock(&mute);
++value;
//printf("productor value=%d\n", value);
pthread_mutex_unlock(&mute);
}
}
void *customer(void *arg){
int i = 0;
while(i < NUM_MAX) {
pthread_mutex_lock(&mute);
i = value;
//printf("customer i=%d\n", i);
pthread_mutex_unlock(&mute);
}
}
int main(){
pthread_t th_productor, th_customer;
int id_productor = 1;
int id_customer = 2;
pthread_mutex_init(&mute, NULL);
pthread_create(&th_productor, NULL, productor, (void *)&id_productor);
pthread_create(&th_customer, NULL, customer, (void *)&id_customer);
pthread_join(th_productor, NULL);
pthread_join(th_customer, NULL);
pthread_mutex_destroy(&mute);
return 0;
}