#include "rwlock.h"

struct read_write_lock rwlock;
int shared_data = 42;

void* reader_func(void* arg) {
    int id = *(int*)arg;
    for (int i = 0; i < 10; i++) {
        ReaderLock(&rwlock);
        // printf("Reader %d read shared_data = %d\n", id, shared_data);
        usleep(1500);
        ReaderUnlock(&rwlock);
        // usleep(50000);
    }
    return NULL;
}

int main() {
    InitalizeReadWriteLock(&rwlock);
    int num_readers = 500;
    pthread_t threads[num_readers];
    int ids[num_readers];

    for (int i = 0; i < num_readers; i++) {
        ids[i] = i + 1;
        pthread_create(&threads[i], NULL, reader_func, &ids[i]);
    }
    for (int i = 0; i < num_readers; i++) {
        pthread_join(threads[i], NULL);
    }

    printf("\nAll readers finished. shared_data unchanged = %d\n", shared_data);
    return 0;
}
