int-supply-1.0.0: A simple, efficient supply of integers using atomic fetch-and-add.
Safe HaskellSafe-Inferred
LanguageHaskell2010

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.

Synopsis

Documentation

data IntSupply Source #

A thread-safe supply of integers.

new :: IO IntSupply Source #

Create a supply of integers.

next :: IntSupply -> IO Int Source #

Get the next integer from a supply of integers.

reset :: IntSupply -> IO () Source #

Reset a supply of integers to 0.