On Tue, Dec 16, 2008 at 5:54 AM, Luke Palmer <span dir="ltr"><<a href="mailto:lrpalmer@gmail.com">lrpalmer@gmail.com</a>></span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Okay, that's a bit clearer. Control.Parallel is not what you want; that is for parallelizing pure code (and is very nice inside its little domain). But you want concurrent code: multiple threads that change state at the same time. That is in Control.Concurrent.<br>
<br>In particular, the "shared variable" is Data.IORef.<br><br>Simple example from which you should be able to extrapolate.</blockquote><div><br>Apologies, this code doesn't compile. Here's the fixed version:<br>
<br><span style="font-family: courier new,monospace;">import Control.Concurrent</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">import Control.Concurrent.MVar</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">import Data.IORef</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">threadBody var = do</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> x <- readIORef var</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> writeIORef var (x+1)</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">main = do</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> var <- newIORef 0</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> -- spawn two threads</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> forkIO (threadBody var)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> forkIO (threadBody var)</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> -- look at the value of the variable</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> x <- readIORef var</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> print x</span><br><br></div></div><br>