Skip to content

Instantly share code, notes, and snippets.

@raichoo
Created September 5, 2017 14:17
Show Gist options
  • Save raichoo/97ed5118f3fd0bc5543cd5010584e998 to your computer and use it in GitHub Desktop.
Save raichoo/97ed5118f3fd0bc5543cd5010584e998 to your computer and use it in GitHub Desktop.
Playing with capabilities.
module Main where
import Control.Monad
import Control.Concurrent
import Text.Printf
import Foreign.C.Types
foreign import ccall safe "getchar" c_getchar :: IO CChar
loop :: Int -> IO ()
loop x = do
forever $ do
tid <- myThreadId
cap <- fst <$> threadCapability tid
c_getchar
cap' <- fst <$> threadCapability tid
when (cap /= cap') $
printf "Thread changed capability: %d\t(%d, %d)\n" x cap cap'
{-
$ ghc -threaded Test.hs -o Test -with-rtsopts=-N2
$ yes | ./Test
-}
main :: IO ()
main = do
forM_ [1..32] $
forkIO . loop
threadDelay (10 * 1000 * 1000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment