stm-data-collection-0.1.0.0: Collection of STM-based Data Structures

Copyright(c) Alex Semin, 2015
LicenseBSD3
Maintaineralllex.semin@gmail.com
Stabilityexperimental
Portabilityportable
Safe HaskellSafe
LanguageHaskell2010

Data.STM.Bag.Class

Description

Concurrent Bag data structure which is relying on STM for consistency in multi-threading environment. Bag (or Multi Set) is a data container which provides very efficient adding and removing but guarantees no order.

import Control.Concurrent.STM
import qualified Data.STM.Bag as Bag

main :: IO ()
main = do
    bag <- atomically $ (Bag.new :: STM (Bag.Impl Int))
    atomically $ Bag.add bag 7
    atomically $ Bag.add bag 5
    x <- atomically $ Bag.take bag
    putStrLn $ show x -- x may be either 5 or 7

Documentation

class Bag b where Source

Methods

new :: STM (b v) Source

O(1). Creates an empty bag.

add :: b v -> v -> STM () Source

O(1). Adds given value to the bag.

take :: b v -> STM v Source

O(1). Returns any item from bag removing it from data structure.

isEmpty :: b v -> STM Bool Source

O(1). Checks whether the bag is empty.