While studying C++, I came accross my lack of training on thread programming. The producer-consumer problem is the first exercise you are asked to solve (and probably the only one worth studying). A complete (and quite careful with potential problems) solution is available here.
To run the program, build it:
$ gcc producer_consumer.c -lm -lpthread -o producer_consumerand then run:
$ ./producer consumer T p cwhere T is the maximum number of "items" to be produced, p is the number of producers and c is the number of consumers.