[Haskell-cafe] Concurrency questions

Andrew Coppin andrewcoppin at btinternet.com
Sun Jan 6 06:45:29 EST 2008


Felipe Lessa wrote:
> On Jan 6, 2008 9:30 AM, Andrew Coppin <andrewcoppin at btinternet.com> wrote:
>   
>> 2. I have a situation where I have a thread generating some data and
>> putting it into a mutable array, and another thread trying to read that
>> data. Is there a way I can make the reader thread block if it tries to
>> read a cell that hasn't been computed yet, but not introduce too much
>> overhead for cells that have been filled in?
>>     
>
> If your thread fills the array linearly, you could maintain a variable
> shared by those threads that say the last cell computed, and have the
> read thread check that before reading. I think this wouldn't create
> too much overhead, although it seems like there must be something
> cleverer somewhere.
>   

That's just it - it fills the array in a fairly random order. (The 
*scanning* is, however, in linear order.)

It's not a big problem - I can wait for the entire array to be filled, 
and then scan it. But I'd like to do both in parallel if there's a 
reasonably easy way to do it.

I suppose an array of MVars would do it, but

1. How big is an MVar?

2. You have to take the data out of an MVar to read it. In other words, 
only 1 thread can read an MVar at once [by design]. This isn't truly a 
problem in the current case, but it's irritating in principle that I 
can't make it so that once the cell is written, multiple threads can 
read it simultaneously...



More information about the Haskell-Cafe mailing list