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
|
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <time.h>
#include <string.h>
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
int sample = 50;
void*
fn(void* arg)
{
char* th = (char*) arg;
int new_sample;
char indent[3] = "";
if (strcmp(th, "B") == 0)
strcpy(indent, "\t");
else if (strcmp(th, "C") == 0)
strcpy(indent, "\t\t");
while (1) {
new_sample = (rand() % 81) + 10;
pthread_mutex_lock(&mutex);
printf("%sSono il thread %s: sample valeva %d, adesso vale %d\n", indent, th, sample, new_sample);
if (sample != new_sample) {
sample = new_sample;
pthread_mutex_unlock(&mutex);
} else {
pthread_mutex_unlock(&mutex);
break;
}
}
printf("%sSono il thread %s, e sto uscendo!\n", indent, th);
pthread_exit(NULL);
}
int
main()
{
pthread_t th[3];
srand(time(NULL));
pthread_create(&th[0], NULL, &fn, (void*) "A");
pthread_create(&th[1], NULL, &fn, (void*) "B");
pthread_create(&th[2], NULL, &fn, (void*) "C");
pthread_join(th[0], NULL);
pthread_join(th[1], NULL);
pthread_join(th[2], NULL);
printf("Il valore finale di sample e' %d\n", sample);
return 0;
}
|