[Haskell-cafe] Re: Haskell-Cafe Digest, Vol 33, Issue 9
Alberto G. Corona
agocorona at gmail.com
Thu May 11 14:17:12 EDT 2006
Hi,
I´m trying to make a searchable transactional cache using STM. The
whole idea is to use indexed TVar variables using a FiniteMap. another
TVar holds the finitemap . This last TVar has to be a global variable.
I found that when handled as global, a TVar does not keep the state.
For example:
stmcache= newTVar 0
main= do
v<- val
v<- val
print v
val = atomically $! do
cache <- stmcache
x <- readTVar cache
writeTVar cache (x+1)
return x
this code print 0 instead of 1 as the result of incrementing cache and
retrieving it the second time
The same happens with:
iocache= atomically $ newTVar 0 `debug` "newTVar"
main= do
v<- val
v<- val
print v
getChar
val = do
cache<- iocache
atomically $! do
x <- readTVar cache
writeTVar cache (x+1)
return x
here i used a IO TVar global instead of a STM TVar
This other version , that passes TVar as parameter, does work:
iocache= atomically $ newTVar 0 `debug` "newTVar"
main= do
cache<-iocache
v<- val cache
v<- val cache
print v
val cache=
atomically $! do
x <- readTVar cache
writeTVar cache (x+1)
I need the global version since the cache acces may be in any location
on the program....
Anyone know a solution using STM? The MVar version works OK, but
blocking the entire index is not the best solution....
More information about the Haskell-Cafe
mailing list