raz / site / content / tour / 15-threads.txt

122 lines · 3.5 KiB · raw · history · last change a5145f64

1title: Threads
2part: Go
3summary: 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
12Two 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
15module Race;
16import Out, Thread;
17import SYSTEM;
18
19var
20 safe: INT; (* added to by XADD *)
21 plain: INT; (* added to by INC: unsafe, never printed *)
22
23proc work(_arg: ^U8);
24var i: INT;
25begin
26 for i := 1 to 100000 do
27 _ := XADD(safe, 1);
28 INC(plain); (* unsafe: lost additions *)
29 end;
30end;
31
32proc main*;
33var t: [4]^Thread.Thread; i: INT; err: ERROR;
34begin
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;
41end;
42
43end Race.
44```
45
46The 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
50An 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
53module Bank;
54import Out, Thread;
55
56var
57 m: Thread.Mutex;
58 balance, deposits: INT; (* kept in step, under m *)
59
60proc deposit(_arg: ^U8);
61var i: INT;
62begin
63 for i := 1 to 10000 do
64 Thread.lock(m);
65 balance := balance + 5;
66 INC(deposits);
67 Thread.unlock(m);
68 end;
69end;
70
71proc main*;
72var t: [3]^Thread.Thread; i: INT; err: ERROR;
73begin
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;
80end;
81
82end Bank.
83```
84
85## Handing work out
86
87A 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
90module Jobs;
91import Out, Thread;
92import SYSTEM;
93
94type Job = record lo, hi: INT; sum: INT; end;
95
96proc work(arg: ^U8);
97var j: ^Job; i: INT;
98begin
99 j := ^Job(arg);
100 for i := j^.lo to j^.hi - 1 do j^.sum := j^.sum + i; end;
101end;
102
103proc main*;
104var jobs: [4]Job; t: [4]^Thread.Thread; i, total: INT; err: ERROR;
105begin
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;
117end;
118
119end 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.