[Haskell-cafe] Opening the same file multiple times

Einar Karttunen ekarttun at cs.helsinki.fi
Mon Dec 12 00:43:15 EST 2005


It seems that opening the same file multiple times (one writer
and multiple readers) is not supported at least on *nix with
GHC. I want to use one Handle to use append data till the
end of the file while other Handles perform random access
IO with seeks on the file.

Sharing the same Handle for all the threads is not possible
since they perform seeks and may thus mess each other up.
Hiding the Handle behind a mutex would limit concurrency
more than I like.

Thus I wanted to open multiple Handles to the file, but
this seems quite hard. My best guess is to create a function 

#ifdef mingw32_HOST_OS
openUnlocked fn mode = openBinaryFile fn mode
openUnlocked fn mode = withMVar mutex $ do
  h  <- openBinaryFile fn mode
  fd <- handleToFd h
  unlockFile $ fromIntegral fd
  return h

{-# NOINLINE mutes #-}
mutex = unsafePerformIO $ newMVar ()

Is there really no simpler solution?

- Einar Karttunen

More information about the Haskell-Cafe mailing list