I want to update a Haskell Gloss GUI from multiple threads (4 threads).
I am looking at an application where an event on the Haskell Gloss
GUI triggers a series of steps that end up creating threads each of which can and should change the GUI if need be. I am using the Gloss play
function.
The Gloss GUI function play
of type:
play
:: Display
-> Color
-> Int
-> world
-> (world -> Picture)
-> (Event -> world -> world)
-> (Float -> world -> world)
-> IO ()
Notice the parameter (Event -> world -> world)
which is the function that handles the keyboard events.
If I click say x on the keyboard, then handleKeys::Event -> world -> world
captures this event
, takes a world
(a model of my application which is a data structure for display by Gloss) and returns a world
with or without changes.
Based on the event processed by handleKeys::Event -> world -> world
other threads may be spawned using forkIO :: IO () -> IO ThreadId
. These spawned threads should also manipulate the world
and return a world
for display. That is each thread runs a function of type world->world
. I would then use STM primitives for concurrency. These primitives are:
putTMVar :: TMVar a -> a -> STM ()
takeMVar :: MVar a -> IO a
atomically :: STM a -> IO a
retry :: STM a
orElse :: STM a -> STM a -> STM a
As you can already guess, the Haskell type-checker is giving me a migraine. Is there possibility of running a Gloss application relying on STM concurrency to update the GUI without violating types?
you should use playIO from Graphics.Gloss.Interface.IO.Game