From f3fb114e7565eefeea3b01ec04825ab52bb65902 Mon Sep 17 00:00:00 2001 From: lukechampine Date: Tue, 5 Apr 2016 15:32:37 -0400 Subject: expand Select example --- README.md | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) (limited to 'README.md') diff --git a/README.md b/README.md index 65f5d36..2ef4e8e 100644 --- a/README.md +++ b/README.md @@ -83,8 +83,25 @@ stm.Atomically(stm.Select( // this function will never run, because the previous // function succeeded - func(tx *stm.Tx) { tx.Retry() }, + func(tx *stm.Tx) { tx.Set(n, 11) }, +)) + +// since Select is a normal transaction, if the entire select retries +// (blocks), it will be retried as a whole: +x := 0 +stm.Atomically(stm.Select( + // this function will run twice, and succeed the second time + func(tx *stm.Tx) { tx.Assert(x == 1) }, + + // this function will run once + func(tx *stm.Tx) { + x = 1 + tx.Retry() + }, )) +// But wait! Transactions are only retried when one of the Vars they read is +// updated. Since x isn't a stm Var, this code will actually block forever -- +// but you get the idea. ``` See [example_santa_test.go](example_santa_test.go) for a more complex example. @@ -92,7 +109,7 @@ See [example_santa_test.go](example_santa_test.go) for a more complex example. ## Pointers Be very careful when managing pointers inside transactions! (This includes -slices, maps, and channels.) Here's why: +slices, maps, channels, and captured variables.) Here's why: ```go p := stm.NewVar([]byte{1,2,3}) @@ -126,8 +143,7 @@ In the same vein, it would be a mistake to do this: type foo struct { i int } -f := &foo{i: 2} -p := stm.NewVar(f) +p := stm.NewVar(&foo{i: 2}) stm.Atomically(func(tx *stm.Tx) { f := tx.Get(p).(*foo) f.i = 7 -- cgit v1.2.3