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

#define SIZE 10000

int b[SIZE];
int in = 0;
int out = 0;

void* producer(void* arg) {
    int v = 0;
    while (true) {
        b[in] = v;
        printf("Produced: %d\n", v);
        v++;
        in++; // Αύξηση δείκτη εισαγωγής
        usleep(500000); // 0.5s delay
    }
    return NULL;
}

void* consumer(void* arg) {
    int w;
    while (true) {
        while (in <= out) /* do nothing - busy wait */; 
        w = b[out];
        printf("Consumed: %d\n", w);
        out++; // Αύξηση δείκτη εξαγωγής
    }
    return NULL;
}

int main() {
    pthread_t prod, cons;
    pthread_create(&prod, NULL, producer, NULL);
    pthread_create(&cons, NULL, consumer, NULL);
    pthread_join(prod, NULL);
    pthread_join(cons, NULL);
    return 0;
}
