I'm facing a problem as an exercise, which is a small variant of classical producer-consumer using two threads. One thread is the producer (P), and the other one the consumer (C).
I have to process a big file, but I can only read it in pieces, like 128 bytes at once. The file contains strings separated by numbers, in a format like this.
[01] this is a string [02] since here it starts another string or sub-string if you wish[02] this is still the first string[01]
P reads from the file, and sends the data to C. If the C is done than the P writes the new data back to the file. The problem is that the elaboration of that data, may not be entirely within that 128 bytes array.
Let's suppose that the C thread need to remove the substring from [02]
to [02]
. But the data array is filled from the first [01]
to the middle of the substring. I cannot check for the 2nd [02]
because it will be read by the producer in a successive call.
I need a way to link between 2 or more pairs of 128-bytes reads. (The sync is not a problem I can of course use critical section and event to handle that.)
My idea is to use a boolean value that keep track if I had something to remove in the current data. But it's not a good way, because [02]
may contain another substring [03]
which needs to be removed. Also the bracket [
may be at array[127] so I don't know if the next read array[0-1] == 02
.
How might I approach this?