title: Can this be done with STM?
body: <p>Disclaimer: This can easily be done using an <code>MVar ()</code> as a simple mutex. I'm just curios to see whether it can be done with STM.</p>
  
  <p>I want to do the following atomically:</p>
  
  <ul>
  <li><p>Read some variables.</p></li>
  <li><p>Decide what I/O to perform, based on what I just read.</p></li>
  <li><p>Perform the I/O.</p></li>
  <li><p>Record the results in the variables.</p></li>
  </ul>
  
  <p>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...)</p>
  
  <p>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.)</p>
  
  <p>Is there a nice solution to this problem? Or is STM simply the wrong tool for this task?</p>