Chapter 33: workers that share nothing, built

The safest concurrent program shares nothing. Eight workers each sum into a variable that only they can see, and the pipeline collects the results - there is no lock to forget, because there is nothing to lock. work is a function type with no body; each worker is a dynamic function that fills it in, capturing its own id. The IDE writes the two constants, the abstract function's signature and the program's shell; you type the workers and the pipeline. To play it in the IDE: Help > Lessons > EK9 in Depth…, or ek9 -ide -lesson depth-worker-totals.

The EK9 IDE as the lesson opens

What you'll build

The lesson builds this program from a file holding only its module line - the finished file, as the book prints it.

#!ek9
defines module book.concurrency.totals

  defines constant
    workerCount <- 8

    depositsPerWorker <- 250

  defines function

    work() as abstract
      <- rtn as Integer?

  defines program

    WorkerTotals()
      stdout <- Stdout()

      workers <- List() of work
      for id in 1 ... workerCount
        worker <- (id) is work as function
          subtotal <- 0
          for n in 1 ... depositsPerWorker
            subtotal += id
          rtn: subtotal
        workers += worker

      subtotals <- cat workers | async | sort | collect as List of Integer
      total <- Integer()
      cat subtotals > total
      stdout.println(`subtotals: ${subtotals}`)
      stdout.println(`total: ${total}`)

Step 1: Constants

Choose Code > New Construct > Constant…

The EK9 IDE once this is done: Choose Code > New Construct > Constant…

workerCount is the constant's name and 8 its value - a literal in the Value field, from which the type is inferred. Type both and choose Preview.

The EK9 IDE just before choosing Preview: workerCount is the constant's name and 8 its value - a literal in the Value field, from which the type is inferred

The preview shows workerCount about to be written. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows workerCount about to be written

Choose Code > New Construct > Constant…

The EK9 IDE once this is done: Choose Code > New Construct > Constant…

depositsPerWorker is the constant's name and 250 its value - a literal in the Value field, from which the type is inferred. Type both and choose Preview.

The EK9 IDE just before choosing Preview: depositsPerWorker is the constant's name and 250 its value - a literal in the Value field, from which the type is inferred

The preview shows depositsPerWorker about to be written. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows depositsPerWorker about to be written

Step 2: work — a function

Choose Code > New Construct > Function…

The EK9 IDE once this is done: Choose Code > New Construct > Function…

work is the function's name. Set Returns to Integer and tick as abstract - a function type: a contract with no body. Choose Preview.

The EK9 IDE just before choosing Preview: work is the function's name

The preview shows work about to be written. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows work about to be written

Select the work() as abstract line and, above it, type the blank line the book opens its first function with. Saves when typed.

The EK9 IDE once this is done: Select the work() as abstract line and, above it, type the blank line the book opens its first function with

Step 3: The program

Choose Code > New Construct > Program…

The EK9 IDE once this is done: Choose Code > New Construct > Program…

WorkerTotals is the program's name. Choose Preview.

The EK9 IDE just before choosing Preview: WorkerTotals is the program's name

The preview shows WorkerTotals about to be written. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows WorkerTotals about to be written

Select the WorkerTotals() line and, above it, type the blank line the book opens the program with. Saves when typed.

The EK9 IDE once this is done: Select the WorkerTotals() line and, above it, type the blank line the book opens the program with

Select the stub, from ready <- true down to its //AI line, and type WorkerTotals's body exactly as the chapter shows it. Saves when typed.

The EK9 IDE once this is done: Select the stub, from ready <- true down to its //AI line, and type WorkerTotals's body exactly as the chapter shows it

Step 4: Run it

Choose Run > Run Program…

The EK9 IDE once this is done: Choose Run > Run Program…

The project's only program is already chosen. Choose Run.

subtotals: [250, 500, 750, 1000, 1250, 1500, 1750, 2000], then total: 9000 - the same every time. Choose Close.

The EK9 IDE just before choosing Close: subtotals

The result

WorkerTotals.ek9 now reads exactly as chapter 33 of EK9 in Depth shows it. Each worker's subtotal is its own; cat ... | async ran the eight together, sort put the answers in order whatever order they finished in, and cat subtotals > total added them.

The EK9 IDE when the lesson has finished

Where this lesson comes from