Relative Content

Tag Archive for clinuxmultithreadingunixconcurrency

Thread syncronization in C producer-consumer problem

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.