-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.c
50 lines (40 loc) · 1.01 KB
/
test.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
#include<stdlib.h>
#include <stdio.h>
#include<pthread.h>
int variabile=100;
pthread_mutex_t scrivi;
pthread_mutex_t leggi = PTHREAD_MUTEX_INITIALIZER;
void *produttore (void *arg)
{
pthread_mutex_lock(&scrivi);
printf("PRODUTTORE: Il valore e': %d\n",variabile);
int n;
printf("Inserire valore da sommare: ");
scanf("%d",&n);
printf("Il valore di n e: %d",n);
variabile=variabile+n;
printf("Variabile condivisa vale: %d",variabile);
pthread_mutex_unlock(&leggi);
pthread_exit(0);
}
void *consumatore (void *arg)
{
pthread_mutex_lock(&leggi);
printf("CONSUMATORE: Il valore e': %d",variabile);
pthread_mutex_unlock(&scrivi);
pthread_exit(0);
}
int main()
{
pthread_t produci;
pthread_t consuma;
pthread_mutex_init(&scrivi,NULL);
pthread_create(&produci,NULL,produttore,NULL);
//printf("Ora passo al consumatore\n");
pthread_create(&consuma,NULL,consumatore,NULL);
pthread_join(produci,NULL);
pthread_join(consuma,NULL);
pthread_mutex_destroy(&scrivi);
pthread_mutex_destroy(&leggi);
return 0;
}