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

sem_t empty, full;
int buffer = 0;

void *producer(void *arg) {
    int item = 1;
    sem_wait(&empty);
    buffer = item;
    printf("生产者生产：%d\n", buffer);
    sem_post(&full);
    return NULL;
}

void *consumer(void *arg) {
    sem_wait(&full);
    printf("消费者消费：%d\n", buffer);
    buffer = 0;
    sem_post(&empty);
    return NULL;
}

int main() {
    sem_init(&empty, 0, 1);
    sem_init(&full, 0, 0);
    pthread_t p, c;
    pthread_create(&p, NULL, producer, NULL);
    pthread_create(&c, NULL, consumer, NULL);
    pthread_join(p, NULL);
    pthread_join(c, NULL);
    sem_destroy(&empty);
    sem_destroy(&full);
    return 0;
}