Disclaimer: This can easily be done using an MVar ()
as a simple mutex. I'm just curios to see whether it can be done with STM.
I want to do the following atomically:
Read some variables.
Decide what I/O to perform, based on what I just read.
Perform the I/O.
Record the results in the variables.
For concreteness, suppose I want to keep track of how many bytes of input I've read, and pretend I've reached EOF after a certain number of bytes have been consumed. (OK, letting two threads read from the same file concurrently is probably a bogus thing to do in the first place, but go with me on this...)
Clearly this cannot be a single STM transaction; there's I/O in the middle. Clearly it would also be wrong to have it as two unconnected transactions. (Two threads could see that there's one byte of quota left, and both decide to read that byte.)
Is there a nice solution to this problem? Or is STM simply the wrong tool for this task?
TChan
... that ought to work. – AuditorySTM
andTChan
. – Gae