title: Haskell concurrency - is forkIO really nondeterministic?
body: <p>I'm trying to use concurrency in Haskell for a specific optimization in which only one of two values is required, and depending on the situation, either one may be much faster to create than the other.</p>
  
  <p>I thought I could just run 2 threads with forkIO, and then wait until a value is placed in an MVar. Here is a simple Test i have written for this:</p>
  
  <pre><code>import Control.Concurrent
  
  main = do out &lt;- newEmptyMVar
            t1 &lt;- forkIO (makeString out)
            t2 &lt;- forkIO (makeInt out)
            v &lt;- takeMVar out
            killThread t1
            killThread t2
            case v of
                 Left s -&gt; putStrLn s
                 Right i -&gt; putStrLn $ show i
  
  makeString out = do s &lt;- return ( show (primes !! 10000))
                      putMVar out $ Left s
  
  makeInt out = do i &lt;- return 2
                   putMVar out $ Right i
  
  primes = sieve [2..] 
   where sieve (x:xs) = x : (sieve $ filter ((/=0).(flip mod x)) xs)
  </code></pre>
  
  <p>Compiled with: </p>
  
  <pre><code>ghc --make -threaded Test
  </code></pre>
  
  <p>However, only the Left s case is ever reached, although getting the prime should take long enough for the makeInt thread to start (and return 2 really shouldn't take that much time). Why is that, and how do I fix this?</p>