The signature of modifyIORef
is straightforward enough:
modifyIORef :: IORef a -> (a -> a) -> IO ()
Unfortunately, this is not thread safe. There is an alternative that adresses this issue:
atomicModifyIORef :: IORef a -> (a -> (a,b)) -> IO b
What exactly are the differences between these two functions? How am I supposed to use the b
parameter when modifying an IORef
that might be read from another thread?
atomicModifyIORef :: IORef a -> (a -> a) -> IO a
, returning the old value, would have served the same purpose (and be simpler, IMO). Interesting. – Flophouse