| Safe Haskell | Safe-Inferred |
|---|---|
| Language | Haskell2010 |
IntSupply
Description
This module provides a simple, efficient supply of integers using atomic fetch-and-add.
To use this module, first create an IntSupply. This is often done once at the top level of an application, in
global scope.
import IntSupply (IntSupply)
import IntSupply qualified
import System.IO.Unsafe (unsafePerformIO)
myIntSupply :: IntSupply
myIntSupply = unsafePerformIO IntSupply.new
{-# NOINLINE myIntSupply #-}Next, call IntSupply.next on the supply, which will return 0, then 1, and so on.
> IntSupply.next myIntSupply 0 > IntSupply.next myIntSupply 1
If desired, you can reset the count to 0.
> IntSupply.reset myIntSupply > IntSupply.next myIntSupply 0
On a 64-bit machine, for many applications, these integers can be treated as effectively unique: even if 1,000,000,000 integers were generated per second, it would still take over 580 years to wrap around.
On a 32-bit machine, more care must be taken, of course: even if only 1,000 integers were generated per second, it would only take 50 days to wrap around.