On Thu, Jan 22, 2009 at 5:48 AM, Ertugrul Soeylemez <es@ertes.de> wrote:
Luke Palmer <
lrpalmer@gmail.com> wrote:
> TVars are overkill here, actually, an IORef would be just fine, I
> think.
Using IORefs is generally a sign of bad style.
I totally disagree.
The disadvantage of IORefs is that they do not work together very nicely. If you have more than one, it is very hard (impossible) to keep multithreaded invariants between them.
But in this case, the IORef is completely encapsulated: it does not leave the scope in which it was created. You will never have to use it with any other IORef, because nobody else even knows it exists. The pattern is self-contained and threadsafe with just an IORef.
A program that needs only IORefs and is threadsafe is *good* style to me, because it means all the state is well-encapsulated (if it escaped, it would fail to be threadsafe).
Luke
But MVars would
perfectly suffice here. They would be essentially the same, but without
'atomically'.