Producer/consumer using pthreads

Here I present a C-program to demonstrate the use of pthreads to solve the classical producer/consumer problem. The following (self-explaining) program makes use of pthread’s mutex & condition APIs to achieve synchronization between producer & consumer threads. The program implements a simplified version of the original problem : the producer increments (produces) an integer, a shared resource, while the consumer prints (consumes) it.

Output:

Consumer : "Hello I am consumer #140317330908928. Ready to consume numbers now"
Producer : "Hello I am producer #140317322516224. Ready to produce numbers now"
Producer : 1
Consumer : 1
Producer : 2
Consumer : 2
Producer : 3
Consumer : 3
Producer : 4
Consumer : 4
Producer : 5
Consumer : 5
Producer : 6
Consumer : 6
Producer : 7
Consumer : 7
Producer : 8
Consumer : 8
Producer : 9
Consumer : 9
Producer : 10
Producer done.. !!
Consumer : 10
Consumer done.. !!
Done..

7 thoughts on “Producer/consumer using pthreads”

  1. Polling on the resource in the consumer until something to consume is available (the while (1), mutex_lock, if (number == 0), mutex_unlock, continue path) is not such a good idea imho.
    You’re probably better off starting with a condition wait.

  2. Brilliant code. Thanks a lot. It’s very useful. I was looking for a similar problem to be rewritten in PHP (pthreads), it gave me a good idea how to use mutex to lock, unlock and do the stuff.

  3. I am thinking how I can make producer to produce twice as fast as consumer and make two consumers to consume them, the above program looks pretty much sequential without much parallelization . Will be glad if someone can help.

    Thanks

  4. I need solution of this
    Please implement the producer consumer problem with a slight variation. In C Program Language

    In this case, we have one producer thread which can produce any random number in the buffer. But there should be two consumer threads.
    One consumer thread will consume only odd values while the second consumer thread will consume only even values from the buffer.

    Both consumer threads and the producer thread should be running concurrently.
    The consumer threads should wait (in a while loop) if there is nothing to be consumed for them.
    The producer thread should wait (in a while loop) if the buffer is full.

    You can include some sleep statements in your program to make the producer and consumers to sleep for a while
    to see your program in action in slow motion. Your program should handle all synchronization issues.

    Please explain which part of your program is critical section and why? Please explain with an example scenario on
    what can go wrong if you do not use proper synchronization tool to protect the critical section?

  5. Please implement the producer consumer problem with a slight variation. In C Program Language

    In this case, we have one producer thread which can produce any random number in the buffer. But there should be two consumer threads.
    One consumer thread will consume only odd values while the second consumer thread will consume only even values from the buffer.

    Both consumer threads and the producer thread should be running concurrently.
    The consumer threads should wait (in a while loop) if there is nothing to be consumed for them.
    The producer thread should wait (in a while loop) if the buffer is full.

    You can include some sleep statements in your program to make the producer and consumers to sleep for a while
    to see your program in action in slow motion. Your program should handle all synchronization issues.

    Please explain which part of your program is critical section and why? Please explain with an example scenario on
    what can go wrong if you do not use proper synchronization tool to protect the critical section?

Leave a Reply

Your email address will not be published. Required fields are marked *