So I have the classic producers-consumers problem that shares a buffer. The shared buffer is a circular queue. The producers are in charge of reading the ‘input’ from another shared array and placing it in the shared buffer. Each producer thread reads a different piece of the array so we distribute equally the work. Then, the consumers must read the entries of the buffer, process them, and put the result in a resulting shared array. My problem is that everything seems to work fine until the moment that the producers won’t write more entries in the shared buffer since there are no more things to read, but the consumers don’t finish since they are waiting for more operations to be in the buffer. But this makes no sense, since (for example), I have 50 operations, and I count how many operations the consumers thread processed, but they keep waiting when, e.g., they are in the 45th operation.
I guess that they wait because the buffer becomes empty, but how can it become empty after processing 45 operations when there are 50? I can’t discover if it happens because not all operations go to the buffer or because not all operations are processed. I need help, thanks in advance.
Since I think it’s better to have all the code instead of me giving some parts of it, here I give a link to the text of all the code. I omit the “queue.h” file since it’s just the declarations of the functions, but I include the definition of the structures queue and element.
https://justpaste.it/dl6ym
You will see some printf()
that I tried to use to debug – you can ignore them. Again, thanks in advance, I know it’s not a straightforward question.