#include<stdio.h>

#include<pthread.h>

int buffer[100];

int count = 0;

int in = -1;

int out = 0;

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

pthread_cond_t buffer_has_space = PTHREAD_COND_INITIALIZER;

pthread_cond_t buffer_has_>PTHREAD_COND_INITIALIZER;

void producer()

{

    int i;

    for(i=0; i<1000; i++)

    {

        pthread_mutex_lock(&mutex);

        if(count == 100)

            pthread_cond_wait(&buffer_has_space, &mutex);

        in++;

        in%=100;

        buffer[in] = i;

        count++;

        pthread_cond_signal(&buffer_has_data);

        pthread_mutex_unlock(&mutex);

    }

   

}

void consumer()

{

    int i, data;

    for(i=0; i<1000; i++)

    {

        pthread_mutex_lock(&mutex);

        if(count==100)

            pthread_cond_wait(&buffer_has_space, &mutex);

        out++;

        out %= 100;

        >buffer[out];

        count--;

        pthread_cond_signal(&buffer_has_data);

        pthread_mutex_unlock(&mutex);

        printf(">, data);

    }

}


int main()

{

    int i;

    pthread_t thread[2];

    pthread_create(&thread[0], NULL, producer, NULL);

    pthread_create(&thread[1], NULL, consumer, NULL);

    for(i=0; i<2; i++)

        pthread_join(thread[i], NULL);

    return 0;

}


mutex의 lock, unlock

thread의 wait, wakeup을 보여줄수 있는 사례라그런가...

아 그리고 concurrent programming은 디버깅 어떤식으로 해야함? 이번에 concurrent는 첨배워서.