Chapter 33: the pair no lock can make atomic, built
The compiler stops a race on a value, a lock taken in the wrong order, a lock handed to another thread. What it does not try to do is make a pair of operations atomic: a check under one lock followed by an act under another is two operations, and nothing is held between them. This Account makes the mistake and the fix side by side. The IDE writes the constant, the class with its lock and each method's shape; you type the four keys and the program that shows the difference. To play it in the IDE: Help > Lessons > EK9 in Depth…, or ek9 -ide -lesson depth-check-then-act.
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.checkthenact defines constant withdrawal <- 80 defines class Account lockedBalance <- MutexLock(100) hasFunds() -> amount as Integer <- rtn as Boolean: false checker <- (amount, rtn) is MutexKey of Integer as function rtn :=: lockedItem >= amount require lockedBalance.enter(checker) take() -> amount as Integer taker <- (amount) is MutexKey of Integer as function lockedItem :=: lockedItem - amount require lockedBalance.enter(taker) takeIfFunds() -> amount as Integer <- rtn as Boolean: false taker <- (amount, rtn) is MutexKey of Integer as function if lockedItem >= amount lockedItem :=: lockedItem - amount rtn :=: true require lockedBalance.enter(taker) balance() <- rtn as Integer: Integer() reader <- (rtn) is MutexKey of Integer as function rtn :=: lockedItem require lockedBalance.enter(reader) default operator ? defines program CheckThenAct() stdout <- Stdout() split <- Account() firstSeesFunds <- split.hasFunds(withdrawal) secondSeesFunds <- split.hasFunds(withdrawal) stdout.println(`both saw enough: ${firstSeesFunds and secondSeesFunds}`) split.take(withdrawal) split.take(withdrawal) stdout.println(`check and take apart: ${split.balance()}`) together <- Account() firstTaken <- together.takeIfFunds(withdrawal) secondTaken <- together.takeIfFunds(withdrawal) stdout.println(`check and take together: ${together.balance()}, taken ${firstTaken} then ${secondTaken}`)
Step 1: A constant
Choose Code > New Construct > Constant…
withdrawal is the constant's name and 80 its value - a literal in the Value field, from which the type is inferred. Type both and choose Preview.
The preview shows withdrawal about to be written. Choose Apply.
Step 2: Account — a balance under a lock
Choose Code > New Construct > Class…
Account is the class's name. Choose Preview.
The preview shows Account about to be written. Choose Apply.
Select the Account line and, above it, type the blank line the book opens its first class with. Saves when typed.
Right-click the Account line and choose Add Field…
Type lockedBalance <- MutexLock(100) - the field and the value it starts with. Choose OK.
The preview shows lockedBalance added - and default operator ? with it, Account's first field. Choose Apply.
Right-click the Account line and choose Add Method…
hasFunds is the method's name. Add one parameter, amount of type Integer, set Return type to Boolean. Choose Apply.
Select the stub and type hasFunds' key: it answers through rtn, which the key captures and changes with :=: - the verb that changes the one object both names share. Saves when typed.
Right-click the Account line and choose Add Method…
take is the method's name. Add one parameter, amount of type Integer. Choose Apply.
Select the stub and type take's key: the balance reduced under the lock. Saves when typed.
Right-click the Account line and choose Add Method…
takeIfFunds is the method's name. Add one parameter, amount of type Integer, set Return type to Boolean. Choose Apply.
Select the stub and type takeIfFunds' key: the check and the take in ONE key, so no one can slip between them. Saves when typed.
Right-click the Account line and choose Add Method…
balance is the method's name. Set Return type to Integer. Choose Apply.
Select the stub and type balance's key. Saves when typed.
Step 3: The program
Choose Code > New Construct > Program…
CheckThenAct is the program's name. Choose Preview.
The preview shows CheckThenAct about to be written. Choose Apply.
Select the CheckThenAct() line and, above it, type the blank line the book opens the program with. Saves when typed.
Select the stub, from ready <- true down to its //AI line, and type CheckThenAct's body exactly as the chapter shows it. Saves when typed.
Step 4: Run it
Choose Run > Run Program…
The project's only program is already chosen. Choose Run.
both saw enough: true, then check and take apart a balance of -60, then together a balance of 20, with taken true then false. Choose Close.
The result
CheckThenAct.ek9 now reads exactly as chapter 33 of EK9 in Depth shows it. hasFunds and take, apart, let two withdrawals both see enough and both go through; takeIfFunds does the check and the take under one enter, so the second finds nothing left. Every operation was correct - the pair was not.
Where this lesson comes from
- Answers -
ek9 -qand the IDE's Answers tab: 213. - For AI assistants - the MCP tool
ek9_new_construct,ek9_add_field,ek9_add_method,ek9_add_program.






























