Doeee
Doeee

Reputation: 43

Reading and writing to different files on the same time - can multithreading be useful?

I'm reading text form a huge file, line by line, perform some operations on them and then store results in different files. If I will have two thread, one that read the data from one file, will other thread be able simultaneously write data to another file? Or as they are o the same hard disk multithreading won't make sense as they will wait for each other?

Upvotes: 0

Views: 463

Answers (1)

Ulrich Eckhardt
Ulrich Eckhardt

Reputation: 17415

If I understand right, you have the three steps

  • read
  • process
  • write

Of course, the hard disk can't perform a read and write operation at the exact same time. However, it could support this:

  • read A (HD)
  • read B (HD), process A (CPU)
  • write A (HD), process B (CPU)
  • read C (HD)
  • write B (HD), process C (CPU)
  • ...

In short, while processing, your program is CPU-bound and the HD is idle. While writing, it is HD-bound (or IO-bound) and the CPU is idle. Interleaving these things can improve overall throughput.

Upvotes: 2

Related Questions