| 1 | title: Threads |
| 2 | part: Go |
| 3 | summary: Native threads without C, atomics, a mutex, and why the counter needs both. |
| 4 | --- |
| 5 | ```raz Hits run |
| 6 | ``` |
| 7 | |
| 8 | `Thread.spawn` runs a procedure in a thread of its own, with a stack of its own, and `Thread.join` waits for it to end. `XADD`, `CAS` and `XCHG` change a word atomically; a `Thread.Mutex` keeps a longer section to one thread at a time. `Out` and `Heap` lock themselves. |
| 9 | |
| 10 | ## What goes wrong without them |
| 11 | |
| 12 | Two threads that each read a variable, add one and write it back can lose one of the additions. A plain `INC` on a variable shared by threads has no particular value afterwards; the report says so. `XADD` is the atomic add: it returns the old value and adds in one indivisible step. |
| 13 | |
| 14 | ```raz Race run |
| 15 | module Race; |
| 16 | import Out, Thread; |
| 17 | import SYSTEM; |
| 18 | |
| 19 | var |
| 20 | safe: INT; (* added to by XADD *) |
| 21 | plain: INT; (* added to by INC: unsafe, never printed *) |
| 22 | |
| 23 | proc work(_arg: ^U8); |
| 24 | var i: INT; |
| 25 | begin |
| 26 | for i := 1 to 100000 do |
| 27 | _ := XADD(safe, 1); |
| 28 | INC(plain); (* unsafe: lost additions *) |
| 29 | end; |
| 30 | end; |
| 31 | |
| 32 | proc main*; |
| 33 | var t: [4]^Thread.Thread; i: INT; err: ERROR; |
| 34 | begin |
| 35 | for i := 0 to 3 do |
| 36 | t[i], err := Thread.spawn(work, nil); |
| 37 | if err # nil then Out.str(MSG(err)); Out.ln; return; end; |
| 38 | end; |
| 39 | for i := 0 to 3 do Thread.join(t[i]); end; |
| 40 | Out.str("safe "); Out.int(safe); Out.ln; |
| 41 | end; |
| 42 | |
| 43 | end Race. |
| 44 | ``` |
| 45 | |
| 46 | The atomic count is always 400000. The plain one is usually lower, and different from run to run, which is why this page doesn't print it. |
| 47 | |
| 48 | ## A mutex for longer sections |
| 49 | |
| 50 | An atomic is one word. When several variables must change together, take a `Thread.Mutex`; it sleeps while it waits, so it suits sections that take a while. Declare it as a module variable, since zero is free. |
| 51 | |
| 52 | ```raz Bank run |
| 53 | module Bank; |
| 54 | import Out, Thread; |
| 55 | |
| 56 | var |
| 57 | m: Thread.Mutex; |
| 58 | balance, deposits: INT; (* kept in step, under m *) |
| 59 | |
| 60 | proc deposit(_arg: ^U8); |
| 61 | var i: INT; |
| 62 | begin |
| 63 | for i := 1 to 10000 do |
| 64 | Thread.lock(m); |
| 65 | balance := balance + 5; |
| 66 | INC(deposits); |
| 67 | Thread.unlock(m); |
| 68 | end; |
| 69 | end; |
| 70 | |
| 71 | proc main*; |
| 72 | var t: [3]^Thread.Thread; i: INT; err: ERROR; |
| 73 | begin |
| 74 | for i := 0 to 2 do |
| 75 | t[i], err := Thread.spawn(deposit, nil); |
| 76 | if err # nil then Out.str(MSG(err)); Out.ln; return; end; |
| 77 | end; |
| 78 | for i := 0 to 2 do Thread.join(t[i]); end; |
| 79 | Out.int(deposits); Out.char(' '); Out.int(balance); Out.ln; |
| 80 | end; |
| 81 | |
| 82 | end Bank. |
| 83 | ``` |
| 84 | |
| 85 | ## Handing work out |
| 86 | |
| 87 | A thread's procedure takes one pointer argument. Pass a record's address to give each thread its own job and its own place to put the answer; then no one shares anything. |
| 88 | |
| 89 | ```raz Jobs run |
| 90 | module Jobs; |
| 91 | import Out, Thread; |
| 92 | import SYSTEM; |
| 93 | |
| 94 | type Job = record lo, hi: INT; sum: INT; end; |
| 95 | |
| 96 | proc work(arg: ^U8); |
| 97 | var j: ^Job; i: INT; |
| 98 | begin |
| 99 | j := ^Job(arg); |
| 100 | for i := j^.lo to j^.hi - 1 do j^.sum := j^.sum + i; end; |
| 101 | end; |
| 102 | |
| 103 | proc main*; |
| 104 | var jobs: [4]Job; t: [4]^Thread.Thread; i, total: INT; err: ERROR; |
| 105 | begin |
| 106 | for i := 0 to 3 do |
| 107 | jobs[i].lo := i * 250000; |
| 108 | jobs[i].hi := (i + 1) * 250000; |
| 109 | t[i], err := Thread.spawn(work, ^U8(SYSTEM.ADR(jobs[i]))); |
| 110 | if err # nil then Out.str(MSG(err)); Out.ln; return; end; |
| 111 | end; |
| 112 | for i := 0 to 3 do |
| 113 | Thread.join(t[i]); |
| 114 | total := total + jobs[i].sum; |
| 115 | end; |
| 116 | Out.int(total); Out.ln; |
| 117 | end; |
| 118 | |
| 119 | end Jobs. |
| 120 | ``` |
| 121 | |
| 122 | `Thread` makes its own threads, without C, so a program that uses it can't use C; one that does makes its threads with C's `pthread_create` instead. The advanced tour goes further into atomics, memory ordering and lock-free code. |