Skip to content

Instantly share code, notes, and snippets.

@graninas
Created October 19, 2017 12:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save graninas/2e172aa210209873134d6d773da86417 to your computer and use it in GitHub Desktop.
Save graninas/2e172aa210209873134d6d773da86417 to your computer and use it in GitHub Desktop.
Dining philosophers and STM
takeFork :: TVar Fork -> STM ()
takeFork tvFork = do
fork <- readTVar tvFork
case fork of
InUse -> retry
Free -> writeTVar tvFork InUse
makeDiningPhilosopher :: TVar Fork -> TVar Fork -> STM (TVar Philosopher)
makeDiningPhilosopher leftFork rightFork = do
takeFork leftFork
takeFork rightFork
newTVar Dining
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment