Re: tryTakeMVar :: MVar a - IO (Maybe a)
Simon Marlow wrote: The only way I can see to do this is to add a lock field to the MVar. And since this lock would look very much like another MVar, you may as well just use another MVar to lock it (after all, that adds exactly zero extra complication to the system :-). Absolutely. There's still some discussion to be had on what putMVar should do when presented with a full MVar. The options are The logical dual to tryTakeMVar would be tryPutMVar :: MVar a - a - IO Bool Or even tryPutMVar :: MVar a - IO a - IO (Maybe a) (the latter only if you can keep the MVar empty while you run the action.) I have absolutely no need for either of these functions, but would either of them be implementable without extra expense?
Re: tryTakeMVar :: MVar a - IO (Maybe a)
Sun, 9 Apr 2000 13:54:47 -0700, Sigbjorn Finne [EMAIL PROTECTED] pisze: Wouldn't be nice to have such function? Have you tried using Concurrent.isEmptyMVar ? isEmptyMVar is not enough to implement tryTakeMVar (but tryTakeMVar would suffice for isEmptyMVar). Just after checking that it is empty I can't safely take the value out of it, because it could be taken by another thread right between and I will sleep for a long time then. tryTakeMVar is a safe variant of isEmptyMVar. The equivalent of pthread_mutex_trylock and Mutex::try_lock in Ruby. -- __("Marcin Kowalczyk * [EMAIL PROTECTED] http://qrczak.ids.net.pl/ \__/ GCS/M d- s+:-- a23 C+++$ UL++$ P+++ L++$ E- ^^ W++ N+++ o? K? w(---) O? M- V? PS-- PE++ Y? PGP+ t QRCZAK 5? X- R tv-- b+++ DI D- G+ e h! r--%++ y-
Re: tryTakeMVar :: MVar a - IO (Maybe a)
Would it be possible to implement an operation to lock an MVar without any extra expense? So that the thread owning the MVar can do things to it, but no other thread can. If it is possible, I suggest that it be added and it would solve Marcin's problem (isEmptyMVar would then suffice).
tryTakeMVar :: MVar a - IO (Maybe a)
Wouldn't be nice to have such function? I wanted to translate a thread example written in Ruby and realized that it's impossible to write that function using MVar primitives provided; and no module seems to provide an equivalent using another kind of concurrent variable. I had to simulate mutexes with MVar (Maybe [MVar ()]) instead of MVar (). I guess that this function should be possible to implement on plain MVars. The semantics is obvious: if the variable is empty, return Nothing, else take its value and return Just it. -- __("Marcin Kowalczyk * [EMAIL PROTECTED] http://qrczak.ids.net.pl/ \__/ GCS/M d- s+:-- a23 C+++$ UL++$ P+++ L++$ E- ^^ W++ N+++ o? K? w(---) O? M- V? PS-- PE++ Y? PGP+ t QRCZAK 5? X- R tv-- b+++ DI D- G+ e h! r--%++ y-