RE: [Haskell-cafe] STM and `orElse` on a few thousand TMVars
On 06 December 2005 10:02, Joel Reymont wrote:
Is it efficient to wait on a few thousand TMVars?
That depends... while waiting your thread consumes no CPU at all (of course), and simply waiting on all those TVars only imposes a small constant overhead on anyone updating one of the TVars (a TMVar is just a TVar). However, checking all those TVars is O(n), and this happens twice each time you wake up: once to find which TVar was modified, and once again to go back to sleep on all of them. I'm not sure what you're using all those TMVars for, but it sounds like it might be better to multiplex them all into a single channel, or something.
How would I write something like that, assuming that my TMVars were in a list.
perhaps something along the lines of foldr orElse (return ()) . map takeTMVar it depends what you want to return. Cheers, Simon
I'm trying to implement a better waitForChildren from the docs for Control.Concurrent. I would like to know when all the children exit, basically, and I thought it would be neat to try to do that with STM. Is there an advantage to STM here? Thanks, Joel On Dec 6, 2005, at 1:29 PM, Simon Marlow wrote:
I'm not sure what you're using all those TMVars for, but it sounds like it might be better to multiplex them all into a single channel, or something.
On 12/6/05, Joel Reymont
I'm trying to implement a better waitForChildren from the docs for Control.Concurrent.
I would like to know when all the children exit, basically, and I thought it would be neat to try to do that with STM.
I apologise if this doesn't make sense (I'm fairly new to Haskell), but wouldn't a single shared counter be sufficient for this? Increment for each child launched. Decrement by each finished child. When it's back down to zero, you're done. Regards, Maarten
Well, I do need to have access to all those thread handles. On Dec 6, 2005, at 2:43 PM, Maarten Hazewinkel wrote:
I apologise if this doesn't make sense (I'm fairly new to Haskell), but wouldn't a single shared counter be sufficient for this?
Increment for each child launched. Decrement by each finished child. When it's back down to zero, you're done.
On Tue, Dec 06, 2005 at 02:52:03PM +0000, Joel Reymont wrote:
Well, I do need to have access to all those thread handles.
A TVar Int and a set/list of handles? Best regards Tomasz -- I am searching for a programmer who is good at least in some of [Haskell, ML, C++, Linux, FreeBSD, math] for work in Warsaw, Poland
On 06.12 20:57, Tomasz Zielonka wrote:
On Tue, Dec 06, 2005 at 02:52:03PM +0000, Joel Reymont wrote:
Well, I do need to have access to all those thread handles.
Since thread creation is inside IO anyways you might want to look at Control.Concurrent.QSem which solves this in an easy fashion. If you want to use STM then a global TVar Int should work fine. - Einar Karttunen
participants (5)
-
Einar Karttunen -
Joel Reymont -
Maarten Hazewinkel -
Simon Marlow -
Tomasz Zielonka