Search code examples
ciodisk-io

Is is possible to write to different parts of the same file from multiple threads?


Can I write to different parts of the same file concurrently from multiple threads (on a typical PC)? I mean there's only one disk head, so the writes can be only performed in some order anyway i.e. not in parallel, right?

Edit:

I'm writing a program that sorts a large binary file but the majority of time is still spent on disk I/O, so I'm just wondering will I gain any extra speed by doing I/O in parallel.


Solution

  • There's nothing to stop you from having multiple threads writing to different parts of the same file.

    I have a program that sorts a large binary file but the majority of time is still spent on disk I/O, so I'm just wondering will I gain any extra speed by doing I/O in parallel.

    If the program is disk-bound, making it multithreaded (and still writing the same amount of data to the same disk) will not speed it up.

    If we are talking about a traditional hard drive, sequential I/O is generally faster than I/O that involves moving the disk head back and forth. With this in mind, splitting the I/O across threads might even be counter-productive.

    There are several avenues to explore as far as speeding things up:

    1. Reducing the amount of I/O (e.g. by employing a sorting algorithm that requires less I/O, or by doing more work in-memory);
    2. Improving I/O throughput, for example by using a faster drive.