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?
Scanning Tunneling Microscopy, or STM, is an imaging technique used to obtain ultra-high resolution images at the atomic scale, without using light or electron beams. STM was invented in 1981 by two IBM scientists named Gerd Binnig and Heinrich Rohrer.
The most severe limitation of scanning tunneling microscopy is the present inability to prepare stable, atomically-sharp tips with known physical and chemical properties.
Because STM is based on measuring the current between the tip and the sample, STM can only analyze conductor and semiconductor samples. Also, because most of the current is generated between the most outward atoms of the tip and the surface, atomic images can be generated only for atomically flat samples.
The STM working principle is quantum tunneling where a tip is moved across the surface of a sample. An image is formed due to variation in tunneling current as the tip moves across the surface. STM offers a good lateral resolution of 0.1 nm and a depth resolution of 0.01 nm.
Use a TVar Bool
named consistent
to keep track of whether your IO action is in progress. Before you run the IO action you set consistent to False
and after you run the IO action you set consistent
to True
. Then, any action that depends on the values of those STM variables that you are modifying just puts this clause at the beginning:
do b <- readTVar consistent
check b
...
This ensures that those actions only see a consistent view of the variables being modified and will not run while the IO action is in progress.
I think you are looking for the stm-io-hooks package.
Whatever you actually want to do – would it be possible to express that in terms of STM's abort/retry semantics? In other words: Can you do a rollback and repeat the IO action? If not, then I'd refer to the answer of Gabriel Gonzalez.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With