ConcurrentUser Guide

Overview

Concurrent builds upon the standard Fantom concurrent library and provides a collection of utility classes for sharing data between threads.

Usage

The Concurrent library provides strategies for sharing data between threads:

Synchronized

Synchronized provides synchronized serial access to a block of code, akin to Java's synchronized keyword. Extend the Synchronized class to use the familiar syntax:

const class Example : Synchronized {
    new make() : super(ActorPool()) { }

    Void main() {
        synchronized |->| {
            // ...
            // important stuff
            // ...
        }
    }
}

Synchronized works by calling the function from within the receive() method of an Actor, which has important implications. First, the passed in function needs to be an immutable func. Next, any object returned also has to be immutable (preferably) or serializable.

Instances of Synchronized may also be used as a mechanism for exclusive locking. For example:

class Example {
    Synchronized lock := Synchronized(ActorPool())

    Void main() {
        lock.synchronized |->| {
            // ...
            // important stuff
            // ...
        }
    }
}

The Concurrent library supplies the following synchronized constructs:

See the individual classes for more details.

Atomic

Atomic Lists and Maps are similar to their Synchronized counterparts in that they are backed by an object held in an AtomicRef. But their write operations are not synchronized. This means they are much more lightweight but it also means they are susceptible to data-loss during race conditions between multiple threads. If used for caching situations where it is not essential for values to exist, this may be acceptable.

See:

Local

Local Refs, Lists and Maps do not share data between threads, in fact, quite the opposite!

They wrap data stored in Actor.locals() thereby constraining it to only be accessed by the executing thread. The data is said to be local to that thread.

The problem is that data held in Actor.locals() is susceptible to being overwritten due to name clashes. Consider:

class Drink {
    Str beer {
      get { Actor.locals["beer"] }
      set { Actor.locals["beer"] = it }
    }
}

man := Drink()
man.beer = "Ale"

kid := Drink()
kid.beer = "Ginger Ale"

echo(man.beer)  // --> Ginger Ale (WRONG!)
echo(kid.beer)  // --> Ginger Ale

To prevent this, LocalRef creates a unique qualified name to store the data under:

class Drink {
    LocalRef beer := LocalRef("beer")
}

man := Drink()
man.beer.val = "Ale"

kid := Drink()
kid.beer.val = "Ginger Ale"

echo(man.beer.val)   // --> Ale
echo(kid.beer.val)   // --> Ginger Ale

echo(man.beer.qname) // --> 0001.beer
echo(kid.beer.qname) // --> 0002.beer

While LocalRefs are not too exciting on their own, BedSheet and IoC use them to keep track of data to be cleaned up at the end of HTTP web requests.

See: