winAPI programming-How two threads process data from in parallel?

How two thread process data from same input file in parallel?Can anyone enlighten how to begin with? There is one textfile, that contains lines of phrases. One thread counts the number of line, and the other thread counts the number or words.

Comments

[color=Blue]That depends on how the algorithm works. I see a few ways to do it:

CASE A)1. Open file and load ALL text into a buffer2. Create threads for counting and pass buffer pointer to each thread

This case does not require a synchronization, because the data in buffer only being read and not written to. However, it will fail for very large files (few Gb), because there will be not enough memory to load the whole file.

CASE 1. Open file2. Create both threads in suspended state3. In a loop do the following: - load a part of file - resume both threads from suspended state - wait for both threads to finish processing data using events, do not ends threads themselves - just the data processing loops - when both threads process all their data - suspend them again and return back to beginning of the loop (load next piece of data, etc.)

In this case you can process file of ANY size, because data being loaded by pieces. You also have to synchronize in a way that BOTH threads are suspended until buffer is being loaded. The buffer is a common data for both threads.

CASE C)1. Simply create both threads and each thread has its own buffer loading code (you can make it into a function) and data counting loop.

In this case you may need to synchronize the file loading. I am not sure if OS allows to read same file on both threads - you can try, but to be safe I would suggest to synchronize it. Meaning, that if one thread loads file - 2nd waits for 1st to finish it. Also, if each thread has its own data buffer - it will increase memory consumption for the code, but Windows has a lot of memory - you can allocate 4Mb for each thread easily and it will not even be noticeable.

Then go to [italic][color=Green]Synchronization[/color][/italic] and then go to [italic][color=Green]Critical-section function[/color][/italic] table. You need to use critical section to make one thread wait until other doing its work on a shared resource. You also can use events and wait for the events to happen, but critical sections are the faster way.[/color]

can you please give a clearer example? i can't still get it. i am new to OS. infact i have just started studying about the subject.Questions:1. What should we use to open file? Is it CreateFile() or the normal FILE *fp, fp= fopen("text.txt", "r") from C; where the file should be opened? is it in the main function?

i did write my own code but still it fails to give the expected result.in my main function, i created two threads. i created also two functionsDWORD WINAPI CountWordsThread() and DWORD WINAPI CountLinesThread(). In both functions i opened the input file.