This works

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
module Main where

import Control.Concurrent
import Control.Monad (forever)
import Control.Monad.ST
import Data.Array.Vanilla.Unsafe

main = do
  let size = 640 * 480
  putStrLn $ "marray_new " ++ show size ++ " 5"
  a <- stToIO $ marray_new size (2, 4, 6)
  b <- stToIO $ marray_freeze a

  forkIO (          writer a); threadDelay (5 * 1000 * 1000)
  forkIO (forever $ reader b)
  getLine
  putStrLn "Exit."

writer a =
  mapM_
    (\ (x, y) -> do
      let t  = fromIntegral (x + y)
      let v1 = floor $ 42 * sin t
      let v2 = floor $ 42 * cos t
      let v3 = floor $ 42 * tan t
      let v  = (v1, v2, v3)
      let n  = x + 640 * y
      putStrLn $ "marray_write a " ++ show n ++ " " ++ show v
      stToIO $ marray_write a n v
    )
    [ (x, y) | y <- [0 .. 480 - 1], x <- [0 .. 640 - 1] ]

reader b =
  mapM_
    (\ (x, y) -> do
      let n = x + 640 * y
      let v = iarray_read b n
      putStrLn $ "iarray_read a " ++ show n ++ " " ++ show v
    )
    [ (x, y) | y <- [0 .. 480 - 1], x <- [0 .. 640 - 1] ]