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

#define THREAD_COUNT 4
#define REPEATS 100000

long long total = 0;
pthread_mutex_t total_lock;

void *count(void *argument) {
    (void)argument;  /* This worker does not need an ID. */
    long long local = 0;
    for (int i = 0; i < REPEATS; i++) local++;
    pthread_mutex_lock(&total_lock);
    total += local;
    pthread_mutex_unlock(&total_lock);
    return NULL;
}

int main(void) {
    pthread_t workers[THREAD_COUNT];
    pthread_mutex_init(&total_lock, NULL);
    for (int t = 0; t < THREAD_COUNT; t++) {
        pthread_create(&workers[t], NULL, count, NULL);
    }
    for (int t = 0; t < THREAD_COUNT; t++) {
        pthread_join(workers[t], NULL);
    }
    printf("Expected: %lld\n", (long long)THREAD_COUNT * REPEATS);
    printf("Observed: %lld\n", total);
    pthread_mutex_destroy(&total_lock);
    return 0;
}
