- #1
Adyssa
- 203
- 3
Homework Statement
Perform file IO with 3 reader and 3 writer threads using a circular buffer. Read the (text) file in and write the same file out.
The Attempt at a Solution
I have an assignment that requires me to use multiple (3) threads to read data from a file, and multiple threads (again, 3) to write this data out to a new file, but the spec is very vague after that and I'm having trouble working out the logic. To compound the issue, I need to use a circular buffer which I have not implemented before.
I'd just like to understand this problem better before I get to coding.
Would it make sense to have each reader thread only concern itself with a portion of the file? So reader_thread_1 read()'s from the start of the file to 1/3 in, and reader_thread_2 reads from 1/3 to 2/3, with reader_thread_3 from 2/3 to the end, and writer_thread_1-3 working in a similar fashion? I will need to pass file offset information to each thread on creation.
With regards to the circular buffer, it seems I will need to wait for reader_thread_1 to complete a full read all the way to 1/3 in, at which time reader_thread_2 can start adding its lines to the buffer. Writer_thread_1 can start consumining lines as soon as they are available but how do I know when to stop, and start writer_thread_2? I'm a bit confused here.
In any case, does this seem like a logical path to take? I realize that this probably isn't the most efficient way to perform File IO but I guess it's an exercise in thread synchronisation first and foremost.