summaryrefslogtreecommitdiff
path: root/Year_3/TSDWL/ex_20191218/thread.c
blob: 7d77646820e989d20879cdd1aa9d0f96b70681e4 (plain)
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
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>

int x;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond;

void*
t1_fn(void* arg)
{
    int m;

    while (1) {
        usleep(100 * 1000);
        m = rand() % 11;

        pthread_mutex_lock(&mutex);
        printf("th1: (x=%d, m=%d):", x, m);
        if (x == -1) {
            break;
        } else if (x == m) {
            printf("RISPOSTA CORRETTA\n");
            x = -1;
            break;
        } else if (abs(x-m) > 5) {
            printf("risposta MOLTO sbagliata\n");
            pthread_mutex_unlock(&mutex);
            pthread_cond_wait(&cond, &mutex);
        } else {
            printf("risposta sbagliata\n");
        }
        pthread_mutex_unlock(&mutex);
    }

    pthread_mutex_unlock(&mutex);
    pthread_exit(NULL);
}

void*
t2_fn(void* arg)
{
    while (1) {
        usleep(300* 1000);
        pthread_mutex_lock(&mutex);
        printf("\tthread 2 %d\n", x);
        if (x == -1) {
            pthread_mutex_unlock(&mutex);
            pthread_exit(NULL);
        }
        pthread_cond_signal(&cond);
        pthread_mutex_unlock(&mutex);
    }
}

int
main()
{
    pthread_t t1, t2;
    srand(time(NULL));
    x = rand() % 11;

    pthread_create(&t1, NULL, t1_fn, NULL);
    pthread_create(&t2, NULL, t2_fn, NULL);
    pthread_join(t1, NULL);
    pthread_join(t2, NULL);

    return 0;
}