home page -> teaching -> parallel and distributed programming -> lab 2- producer-consumer synchronization

Lab 2 - Producer-consumer synchronization

Due: week 5.

Goal

Create two threads, a producer and a consumer, with the producer feeding the consumer.

Requirement: Compute the scalar product of two vectors.

Create two threads. The first thread (producer) will compute the products of pairs of elements - one from each vector - and will feed the second thread. The second thread (consumer) will sum up the products computed by the first one.

You shall have a queue, of configurable size, holding the elements between the producer and the consumer. The two threads will be synchronized with one or two condition variables and a mutex. The consumer will be cleared to use each product as soon as it is computed by the producer thread. Meanwhile, the producer will be put to wait if the queue reaches the maximum allowed size.

Play with the queue size and measure the time needed for the program to complete.

Radu-Lucian LUPŞA
2025-10-05