thread safety, IO arrays, and IO refs

Hello, I wish to use a mutable array in multiple threads. Can IO arrays be used in any thread, or only the thread they are created in? (So if I create an IO array in one thread, pass it to another via an MVar, can I read / edit it in that other thread?) Similarly about IORefs... can they be used across threads? (Obviously I will need to manage locking myself if I use an IO ref or array.) Are there any good resources on concurrency in Haskell that would answer questions similar to these? I've been learning by reading the ghc library documentation, but of course it's not comprehensive. The online tutorials I've seen aren't so in-depth, either. Thanks, Eric

Have you tried it? Did you run in to any trouble?
Assuming you get locking correct there shouldn't be any trouble with this.
Take care,
Antoine
On Fri, Dec 31, 2010 at 4:19 AM, Eric Stansifer
Hello,
I wish to use a mutable array in multiple threads. Can IO arrays be used in any thread, or only the thread they are created in? (So if I create an IO array in one thread, pass it to another via an MVar, can I read / edit it in that other thread?) Similarly about IORefs... can they be used across threads?
(Obviously I will need to manage locking myself if I use an IO ref or array.)
Are there any good resources on concurrency in Haskell that would answer questions similar to these? I've been learning by reading the ghc library documentation, but of course it's not comprehensive. The online tutorials I've seen aren't so in-depth, either.
Thanks, Eric
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

I tried a quick test with an IOArray, so I know that at least one time it works. I don't know enough of the internals of IO and IOArray to be able to extrapolate; there could be some race condition hidden internally if IO is misused. Thanks, Eric

If you need multithreaded mutable arrays, TArray may be a better choice, as the underlying stm implementation will manage locking for you. (The current implementation isn't terribly efficient, but when it gets better you'll get it for free too :-) http://hackage.haskell.org/packages/archive/stm/2.2.0.1/doc/html/Control-Con... Edward

On 31/12/2010 09:19, Eric Stansifer wrote:
Hello,
I wish to use a mutable array in multiple threads. Can IO arrays be used in any thread, or only the thread they are created in? (So if I create an IO array in one thread, pass it to another via an MVar, can I read / edit it in that other thread?) Similarly about IORefs... can they be used across threads?
There are no restrictions, you can use an IOArray or IORef in any thread. Cheers, Simon
participants (4)
-
Antoine Latter
-
Edward Z. Yang
-
Eric Stansifer
-
Simon Marlow