#include <stdio.h>
#include <unistd.h>
#include <pthread.h>

#include "mutex.h"

enum {
    num_threads = 3,
    pause_seconds = 1
};

typedef struct {
    int val;
    int x;
} data_t;

static void *worker_thread(void *data)
{
    data_t *d = data;

    lock(&d->val);
    d->x++;
    unlock(&d->val);

    return NULL;
}

int main()
{
    int i;
    pthread_t t;
    data_t d;

    d.val = 0;
    d.x = 0;

    for (i = 0; i < num_threads; i++)
        pthread_create(&t, NULL, worker_thread, &d);

    while (1) {
        lock(&d.val);
        printf("%d\n", d.x);
        unlock(&d.val);
        sleep(pause_seconds);
    }

    return 0;
}
