I quote Simon Peyton-Jones: "STM requires runtime logging of every memory read and write. In an imperative language (such as C#), in which the very fabric of computation involves reads and writes, STM is bound to be expensive. [Lots of work has been done to omit redundant logging based on program analysis, but it's still expensive.] When do you need readTVar/writeTVar? Answer, precisely when you are manipulating state shared between threads."
It looks like STM works better for functional languages and you have to mark shared data somehow. And then you have to mark the code that manipulates shared data somehow (so it won't prematurely launch missiles). Then you're reinvented monad.
I reserve right to be wrong so I'd like to see what are the ways for imperative languages.
With the same performance, static protection against irreversible side-effects, and clever aversion of the privatization problem? I'm not aware of any. While STM can be theoretically done in any memory-safe imperative language, Haskell -- mainly because of its purity and type system -- seems to be the only language in which it can be fast enough and correct enough for practical use.
edit: well, Clojure restricts mutability too, so STM is sort of tractable there, but it doesn't get the other benefits that Haskell's type system gives you.
Comments
I don't understand why you need monads to get STM. orElse is neat, but there are other ways to do the same thing in imperative languages.
I quote Simon Peyton-Jones: "STM requires runtime logging of every memory read and write. In an imperative language (such as C#), in which the very fabric of computation involves reads and writes, STM is bound to be expensive. [Lots of work has been done to omit redundant logging based on program analysis, but it's still expensive.] When do you need readTVar/writeTVar? Answer, precisely when you are manipulating state shared between threads."
http://www.0x61.com/forum/programming-haskell-cafe-f245/is-t...
It looks like STM works better for functional languages and you have to mark shared data somehow. And then you have to mark the code that manipulates shared data somehow (so it won't prematurely launch missiles). Then you're reinvented monad.
I reserve right to be wrong so I'd like to see what are the ways for imperative languages.
Oh, tons of work has been done in trying to bring STM to imperative languages, most notably with STM.NET (which is what SPJ's referring to there).
With the same performance, static protection against irreversible side-effects, and clever aversion of the privatization problem? I'm not aware of any. While STM can be theoretically done in any memory-safe imperative language, Haskell -- mainly because of its purity and type system -- seems to be the only language in which it can be fast enough and correct enough for practical use.
edit: well, Clojure restricts mutability too, so STM is sort of tractable there, but it doesn't get the other benefits that Haskell's type system gives you.