diff options
author | Chris Wendt <chrismwendt@gmail.com> | 2022-06-08 02:28:37 -0600 |
---|---|---|
committer | Chris Wendt <chrismwendt@gmail.com> | 2022-06-08 03:02:44 -0600 |
commit | 30943ded71e123886291ad393e55bfb6aa837df3 (patch) | |
tree | ca7cc2520959386d6c57d9204f284be217bb0b72 /doc_test.go | |
parent | use generic atomic (diff) | |
download | stm-30943ded71e123886291ad393e55bfb6aa837df3.tar.gz stm-30943ded71e123886291ad393e55bfb6aa837df3.tar.xz |
BIG change: generic Var[T], txVar, etc.
Diffstat (limited to 'doc_test.go')
-rw-r--r-- | doc_test.go | 24 |
1 files changed, 12 insertions, 12 deletions
diff --git a/doc_test.go b/doc_test.go index f6bb863..670d07b 100644 --- a/doc_test.go +++ b/doc_test.go @@ -6,43 +6,43 @@ import ( func Example() { // create a shared variable - n := stm.NewVar(3) + n := stm.NewVar[int](3) // read a variable var v int stm.Atomically(stm.VoidOperation(func(tx *stm.Tx) { - v = tx.Get(n).(int) + v = n.Get(tx) })) // or: - v = stm.AtomicGet(n).(int) + v = stm.AtomicGet(n) _ = v // write to a variable stm.Atomically(stm.VoidOperation(func(tx *stm.Tx) { - tx.Set(n, 12) + n.Set(tx, 12) })) // or: stm.AtomicSet(n, 12) // update a variable stm.Atomically(stm.VoidOperation(func(tx *stm.Tx) { - cur := tx.Get(n).(int) - tx.Set(n, cur-1) + cur := n.Get(tx) + n.Set(tx, cur-1) })) // block until a condition is met stm.Atomically(stm.VoidOperation(func(tx *stm.Tx) { - cur := tx.Get(n).(int) + cur := n.Get(tx) if cur != 0 { tx.Retry() } - tx.Set(n, 10) + n.Set(tx, 10) })) // or: stm.Atomically(stm.VoidOperation(func(tx *stm.Tx) { - cur := tx.Get(n).(int) + cur := n.Get(tx) tx.Assert(cur == 0) - tx.Set(n, 10) + n.Set(tx, 10) })) // select among multiple (potentially blocking) transactions @@ -51,11 +51,11 @@ func Example() { stm.VoidOperation(func(tx *stm.Tx) { tx.Retry() }), // this function will always succeed without blocking - stm.VoidOperation(func(tx *stm.Tx) { tx.Set(n, 10) }), + stm.VoidOperation(func(tx *stm.Tx) { n.Set(tx, 10) }), // this function will never run, because the previous // function succeeded - stm.VoidOperation(func(tx *stm.Tx) { tx.Set(n, 11) }), + stm.VoidOperation(func(tx *stm.Tx) { n.Set(tx, 11) }), )) // since Select is a normal transaction, if the entire select retries |