O'Reilly logo

Haskell Cookbook by Yogesh Sajanikar

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How it works...

The IORef a represents a mutable reference in an the IO monad. In this recipe, we used forkIO run an IO() action for tracker in a separate thread. The function work runs in the main thread. It uses atomicModifyIORef' to atomically increment the counter. This function will prevent race conditions for one IORef. Note that there are two versions of atomicModifyIORef. The atomicModifyIORef version is a lazy version (the operation done may not be evaluated immediately), whereas atomicModifyIORef' is a strict version. Finally, we kill the tracker thread with the killThread function.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required