#include #include #include sem_t can_produce; sem_t can_consume; void *producer(void *data) { while (1) { sem_wait(&can_produce); puts("PRODUCING!"); sem_post(&can_consume); } } void *consumer(void *data) { while (1) { sem_wait(&can_consume); puts("CONSUMING!"); sem_post(&can_produce); } } int main(int argc, char **argv) { pthread_t thread_prod, thread_cons; sem_init(&can_produce, 0, 1); sem_init(&can_consume, 0, 0); pthread_create(&thread_prod, NULL, producer, NULL); pthread_create(&thread_cons, NULL, consumer, NULL); pthread_join(thread_cons, NULL); pthread_join(thread_prod, NULL); return 0; }