1

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?

Gakuo
  • 845
  • 6
  • 26
  • 1
    Can't write an answer but you should use `playIO` from `Graphics.Gloss.Interface.IO.Game` – monocell Jan 11 '19 at 08:09
  • @monocell Valuable information, thumbs up. – Gakuo Jan 11 '19 at 08:41
  • This does not answer your question, but an alternate solution to updating gloss with multiple threads is to use the parallel library (which does not require IO) instead of the STM library. – PyRulez Sep 07 '19 at 21:49

1 Answers1

1

you should use playIO from Graphics.Gloss.Interface.IO.Game

Gakuo
  • 845
  • 6
  • 26