The problem is that it is not possible to determine the number of iterations at the beginning of the loop. Knowing where each line starts requires having read the previous line and knowing the total number of lines requires that all lines have already been read. Inside the loop you wrote count++;
. That is, to know the value of count
and increment it needs that the previous iteration has already been completed. Finally, there is nothing parallelizable in this code.
Some solutions to this can be:
Read all the lines of the file in a previous and eternal array over the Paralelized form array in a similar way to how you intended to do.
Map the file into memory pages (the operating system has functions for this), identify the start and size of each line, and store this pair of integers in an array. Finally eternal over the shape array.
Create a production thread that will read the file line by line and one or more consumer threads, which will process the read lines. Here I don’t think Openmp will help, but the standard library has primitive classes that can help.
I am assuming, of course, that the processing of each line is much more costly than the act of reading the file line. This is, however, unlikely. Read/write operations on the disk are the bottleneck in most cases, and you cannot parallelize the disk.
Do you really need the library? You can do this with pure C++ . See my answer.
– user2692
@Lucashenrique, I do need.
– Macario1983