ããã¯ãHaskellスペースリーク Advent Calendar 2015ã®7æ¥ç®ã®è¨äºã§ãã
å㯠Haskell ã§ä¸»ã« IO ãªã³ã¼ããæ¸ãã¦ãããããããã¾ãã¹ãã¼ã¹ãªã¼ã¯ãèµ·ããããã¨ããªããããã¾ã§èµ·ãããå¯ä¸ã®ã¹ãã¼ã¹ãªã¼ã¯ã¯ããµã¼ãããã°ã©ã ã®ä¸ã®ä»¥ä¸ã®ãããªã³ã¼ãã ã£ãã
atomicModifyIORef ref (\_ -> (tmstr, ()))
ããã¯ãæéãæååã«å¤æã㦠IORef ã«ãã£ãã·ã¥ãããã³ã¼ãã ãããã¹ã¬ããããæ¯ç§ãã®ã³ã¼ããå¼ã³åºããä¸æè°ãªãã¨ã«ãã¯ã©ã¤ã¢ã³ãããã®ã¢ã¯ã»ã¹ãããã¨ã¹ãã¼ã¹ãªã¼ã¯ã«ã¯ãªããªãã®ã ããé·æéã¢ã¯ã»ã¹ããªãã¨ãµã¼ãã®ããã»ã¹ã太ã£ã¦ããã
ãã®ãªã¼ã¯ã¯ãçµæãå©ç¨ãã¦ããªãããã«èµ·ãããããããããã®ã³ã¼ãã¯å¤ãå¤ãå©ç¨ããã«ãåã«æ°ããå¤ã§ç½®ãæãã¦ããã ãã ãããwriteIORef ã§ååã ã
writeIORef ref tmstr
ã§ã¯ãã«ã¦ã³ã¿ã¼ã®ããã«å¤ãå¤ã使ãå ´åã¯ã©ãããã°ããã ããï¼
atomicModifyIORef ref (\i -> (i+1, ()))
ãã®ã¹ãã¼ã¹ãªã¼ã¯ããªããããã®å®ç³ã¯ãçµæãè©ä¾¡ãã¦ãããã¨ã§ããã
x <- atomicModifyIORef ref (\i -> (i+1, ())) x `seq` return ()
BanPatterns ã使ãã¨ããããã¹ãããªããã
!_ <- atomicModifyIORef ref (\i -> (i+1, ()))
ä¸çªããã®ã¯ãCAS ã§å¤ãç½®ãæããã¨ãã¯é 延è©ä¾¡ã§ãCAS ãæåãå¾ã«æ£æ ¼è©ä¾¡ãã atomicModifyIORef' ã使ããã¨ã ã
atomicModifyIORef' ref (\i -> (i+1, ()))
ãã¾ã
æãã®è©±ã Monad.Reader 19ã«æ¸ããã¨ãããatomicModifyIORef ã®ããã¥ã¢ã«ã«æ³¨ææ¸ãã足ãããã:-)
atomicModifyIORef' ã¯ãä¸¦è¡ Haskell ã®å¥¥ç¾©ã§ããã詳ããã¯ãHaskellによる並列・並行プログラミングããèªãã§ã»ããã