2 Replies
      Latest reply on May 14, 2020 10:45 AM by john daniel
      Shagpile Level 1 Level 1 (0 points)

        https://developer.apple.com/documentation/foundation/filehandle

         

        I'm reading non-sequential bytes (random records) from a large file (hundreds of megabytes, low gigabytes) rapidly across several threads. Each thread may use a seek then read multiple times when reading records.

         

        It is expensive constantly creating new FileHandle instances for each small read. I want to where possible reuse the same FileHandle instance without using locking to control access (I'm currently using locking to control access to shared FileHandle which works okay, but is not maximum performance...).

         

        I want to avoid a race condition where one thread does a seek right before another does a read, changing the file offset in a shared FileHandler reader.

         

        It appears that pread() handles this nicely:

        http://man7.org/linux/man-pages/man2/pwrite.2.html

         

        >> The pread() and pwrite() system calls are especially useful in

        >>  multithreaded applications. They allow multiple threads to perform

        >>  I/O on the same file descriptor without being affected by changes to

        >> the file offset by other threads.

         

        Is FileHandle opened via init?(forReadingAtPath path: String) safe to call the seek and read functions in this manner?  i.e. does it use pread() under the hood and can I use a shared FileHandle instance across multiple threads for random reading without the file offset being hosed by another thread?