What have you found for these years?


thread-safe is not fiber-safe; fiber-safe could be even harder (4)

After chatting with Favonia, I finally realized that the previous approach is wrong.
Coroutine (fiber) is wrong, lock is also wrong. We don't really need lock (MVar)
if we have STM and TMVar. Yes, the key is TMVar, which is a GHC only extension:

takeTMVar :: TMVar a -> STM a
Return the contents of the TMVar. If the TMVar is currently empty,
the transaction will retry. After a takeTMVar, the TMVar is left empty.

After I read this, I knew this should be the solution. I'd thought about something
like this before, and actually I did use the same mechanism when solving
dining philosophers. And that's also why I felt we still needed MVar somehow
even if we have STM. I just didn't implement this as a datatype, which I should.
As you can see, it's not hard to implement one: Here's the source of TMVar.

I have no idea what is CPP, DeriveDataTypeable, and Trustworthy though.
Anyway, you get the idea...

I'm quite tired right now though. I'll write why now I think coroutine is the
wrong approach in the next post, after I can find a proving example.

I do very appreciate Favonia. That's pretty enlightening. I am not sure
if this can be applied to Ruby, too, though, given that Haskell is really
powerful in this regard...

0 retries:

Post a Comment

Note: Only a member of this blog may post a comment.

All texts are licensed under CC Attribution 3.0