Debian Codegraph @

doc/hpc/chap5_mj.html

raw symbols SCIP
1 <?xml version="1.0" encoding="UTF-8"?>
2
3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
4 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
5
6 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
7 <head>
8 <script type="text/javascript"
9 src="https://cdn.jsdelivr.net/npm/mathjax@2/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
10 </script>
11 <title>GAP (hpc) - Chapter 5: Atomic objects</title>
12 <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
13 <meta name="generator" content="GAPDoc2HTML" />
14 <link rel="stylesheet" type="text/css" href="manual.css" />
15 <script src="manual.js" type="text/javascript"></script>
16 <script type="text/javascript">overwriteStyle();</script>
17 </head>
18 <body class="chap5" onload="jscontent()">
19
20
21 <div class="chlinktop"><span class="chlink1">Goto Chapter: </span><a href="chap0_mj.html">Top</a> <a href="chap1_mj.html">1</a> <a href="chap2_mj.html">2</a> <a href="chap3_mj.html">3</a> <a href="chap4_mj.html">4</a> <a href="chap5_mj.html">5</a> <a href="chap6_mj.html">6</a> <a href="chap7_mj.html">7</a> <a href="chap8_mj.html">8</a> <a href="chap9_mj.html">9</a> <a href="chap10_mj.html">10</a> <a href="chap11_mj.html">11</a> <a href="chapInd_mj.html">Ind</a> </div>
22
23 <div class="chlinkprevnexttop">&nbsp;<a href="chap0_mj.html">[Top of Book]</a>&nbsp; <a href="chap0_mj.html#contents">[Contents]</a>&nbsp; &nbsp;<a href="chap4_mj.html">[Previous Chapter]</a>&nbsp; &nbsp;<a href="chap6_mj.html">[Next Chapter]</a>&nbsp; </div>
24
25 <p id="mathjaxlink" class="pcenter"><a href="chap5.html">[MathJax off]</a></p>
26 <p><a id="X7A5E986A790A5A18" name="X7A5E986A790A5A18"></a></p>
27 <div class="ChapSects"><a href="chap5_mj.html#X7A5E986A790A5A18">5 <span class="Heading">Atomic objects</span></a>
28 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap5_mj.html#X79DFD10A80868799">5.1 <span class="Heading">Atomic lists</span></a>
29 </span>
30 <div class="ContSSBlock">
31 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap5_mj.html#X7F1353B58414D4C3">5.1-1 AtomicList</a></span>
32 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap5_mj.html#X8279EC4483E0562C">5.1-2 FixedAtomicList</a></span>
33 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap5_mj.html#X807687A579422D3B">5.1-3 MakeFixedAtomicList</a></span>
34 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap5_mj.html#X81AF8061871DA27C">5.1-4 FromAtomicList</a></span>
35 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap5_mj.html#X795EE57983074ADF">5.1-5 ATOMIC_ADDITION</a></span>
36 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap5_mj.html#X7847EAA284A02AEE">5.1-6 COMPARE_AND_SWAP</a></span>
37 </div></div>
38 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap5_mj.html#X823767367D6CF4FD">5.2 <span class="Heading">Atomic records and component objects</span></a>
39 </span>
40 <div class="ContSSBlock">
41 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap5_mj.html#X87EA343179BE4BFE">5.2-1 AtomicRecord</a></span>
42 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap5_mj.html#X81E6B21C7A1AC7FE">5.2-2 FromAtomicRecord</a></span>
43 </div></div>
44 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap5_mj.html#X78EDB1DB79473F53">5.3 <span class="Heading">Replacement policy functions</span></a>
45 </span>
46 <div class="ContSSBlock">
47 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap5_mj.html#X7B76F2AD818492BC">5.3-1 MakeWriteOnceAtomic</a></span>
48 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap5_mj.html#X806C3F0681B847A0">5.3-2 MakeStrictWriteOnceAtomic</a></span>
49 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap5_mj.html#X7E7591EC780F3988">5.3-3 MakeReadWriteAtomic</a></span>
50 </div></div>
51 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap5_mj.html#X7DCE3B4D7D26407F">5.4 <span class="Heading">Thread-local records</span></a>
52 </span>
53 <div class="ContSSBlock">
54 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap5_mj.html#X7BE7D67586FC91E6">5.4-1 ThreadLocalRecord</a></span>
55 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap5_mj.html#X788AF1CC8111798C">5.4-2 SetTLDefault</a></span>
56 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap5_mj.html#X853A898384888B85">5.4-3 SetTLConstructor</a></span>
57 </div></div>
58 </div>
59
60 <h3>5 <span class="Heading">Atomic objects</span></h3>
61
62 <p>HPC-GAP provides a number of atomic object types. These can be accessed by multiple threads concurrently without requiring explicit synchronization, but can have non-deterministic behavior for complex operations. Atomic lists are fixed-size lists; they can be assigned to and read from like normal plain lists. Atomic records are atomic versions of plain records. Unlike plain records, though, it is not possible to delete elements from an atomic record. The primary use of atomic lists and records is to facilitate storing the result of idempotent operations and to support certain low-level operations. Atomic lists and records can have three different replacement policies: write-once, strict write-once, and rewritable. The replacement policy determines whether an already assigned element can be changed. The write-once policy allows elements to be assigned only once, with subsequent assignments being ignored; the strict write-once policy allows elements also to be assigned only once, but subsequent assignments will raise an error; the rewritable policy allows elements to be assigned different values repeatedly. The default for new atomic objects is to be rewritable. Thread-local records are variants of plain records that are replicated on a per-thread basis.</p>
63
64 <p><a id="X79DFD10A80868799" name="X79DFD10A80868799"></a></p>
65
66 <h4>5.1 <span class="Heading">Atomic lists</span></h4>
67
68 <p>Atomic lists are created using the <code class="code">AtomicList</code> or <code class="code">FixedAtomicList</code> functions. After creation, they can be used exactly like any other list, except that atomic lists created with <code class="code">FixedAtomicList</code> cannot be resized. Their contents can also be read as normal plain lists using <code class="code">FromAtomicList</code>.</p>
69
70
71 <div class="example"><pre>
72 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">a := AtomicList([1,2,4]);</span>
73 &lt;atomic list of size 3&gt;
74 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">WaitTask(RunTask(function() a[1] := a[1] + a[2]; end));</span>
75 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">a[1];</span>
76 3
77 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">FromAtomicList(a);</span>
78 [ 3, 2, 4 ]
79 </pre></div>
80
81 <p>Because multiple threads can read and write the list concurrently without synchronization, the results of modifying the list may be non-deterministic. It is faster to write to fixed atomic lists than to a resizable atomic list.</p>
82
83 <p><a id="X7F1353B58414D4C3" name="X7F1353B58414D4C3"></a></p>
84
85 <h5>5.1-1 AtomicList</h5>
86
87 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; AtomicList</code>( <var class="Arg">list</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
88 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; AtomicList</code>( <var class="Arg">count</var>, <var class="Arg">obj</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
89 <p><code class="func">AtomicList</code> is used to create a new atomic list. It takes either a plain list as an argument, in which case it will create a new atomic list of the same size, populated by the same elements; or it takes a count and an object argument. In that case, it creates an atomic list with <var class="Arg">count</var> elements, each set to the value of <var class="Arg">obj</var>.</p>
90
91
92 <div class="example"><pre>
93 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">al := AtomicList([3, 1, 4]);</span>
94 &lt;atomic list of size 3&gt;
95 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">al[3];</span>
96 4
97 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">al := AtomicList(10, `"alpha");</span>
98 &lt;atomic list of size 10&gt;
99 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">al[3];</span>
100 "alpha"
101 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">WaitTask(RunTask(function() al[3] := `"beta"; end));</span>
102 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">al[3];</span>
103 "beta"
104 </pre></div>
105
106 <p><a id="X8279EC4483E0562C" name="X8279EC4483E0562C"></a></p>
107
108 <h5>5.1-2 FixedAtomicList</h5>
109
110 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; FixedAtomicList</code>( <var class="Arg">list</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
111 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; FixedAtomicList</code>( <var class="Arg">count</var>, <var class="Arg">obj</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
112 <p><code class="func">FixedAtomicList</code> works like <code class="func">AtomicList</code> (<a href="chap5_mj.html#X7F1353B58414D4C3"><span class="RefLink">5.1-1</span></a>) except that the resulting list cannot be resized.</p>
113
114 <p><a id="X807687A579422D3B" name="X807687A579422D3B"></a></p>
115
116 <h5>5.1-3 MakeFixedAtomicList</h5>
117
118 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; MakeFixedAtomicList</code>( <var class="Arg">list</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
119 <p><code class="func">MakeFixedAtomicList</code> turns a resizable atomic list into a fixed atomic list.</p>
120
121
122 <div class="example"><pre>
123 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">a := AtomicList([99]);</span>
124 &lt;atomic list of size 1&gt;
125 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">a[2] := 100;</span>
126 100
127 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">MakeFixedAtomicList(a);</span>
128 &lt;fixed atomic list of size 2&gt;
129 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">a[3] := 101;</span>
130 Error, Atomic List Element: &lt;pos&gt;=3 is an invalid index for &lt;list&gt;
131 </pre></div>
132
133 <p><a id="X81AF8061871DA27C" name="X81AF8061871DA27C"></a></p>
134
135 <h5>5.1-4 FromAtomicList</h5>
136
137 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; FromAtomicList</code>( <var class="Arg">atomic_list</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
138 <p><code class="func">FromAtomicList</code> returns a plain list containing the same elements as <var class="Arg">atomic_list</var> at the time of the call. Because other threads can write concurrently to that list, the result is not guaranteed to be deterministic.</p>
139
140
141 <div class="example"><pre>
142 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">al := AtomicList([10, 20, 30]);;</span>
143 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">WaitTask(RunTask(function() al[2] := 40; end));</span>
144 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">FromAtomicList(al);</span>
145 [ 10, 40, 30 ]
146 </pre></div>
147
148 <p><a id="X795EE57983074ADF" name="X795EE57983074ADF"></a></p>
149
150 <h5>5.1-5 ATOMIC_ADDITION</h5>
151
152 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; ATOMIC_ADDITION</code>( <var class="Arg">atomic_list</var>, <var class="Arg">index</var>, <var class="Arg">value</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
153 <p><code class="func">ATOMIC_ADDITION</code> is a low-level operation that atomically adds <var class="Arg">value</var> to <var class="Arg">atomic_list[index]</var>. It returns the value of <var class="Arg">atomic_list[index]</var> after the addition has been performed.</p>
154
155
156 <div class="example"><pre>
157 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">al := FixedAtomicList([4,5,6]);;</span>
158 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">ATOMIC_ADDITION(al, 2, 7);</span>
159 12
160 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">FromAtomicList(al);</span>
161 [ 4, 12, 6 ]
162 </pre></div>
163
164 <p><a id="X7847EAA284A02AEE" name="X7847EAA284A02AEE"></a></p>
165
166 <h5>5.1-6 COMPARE_AND_SWAP</h5>
167
168 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; COMPARE_AND_SWAP</code>( <var class="Arg">atomic_list</var>, <var class="Arg">index</var>, <var class="Arg">old</var>, <var class="Arg">new</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
169 <p><code class="func">COMPARE_AND_SWAP</code> is an atomic operation. It atomically compares <var class="Arg">atomic_list[index]</var> to <var class="Arg">old</var> and, if they are identical, replaces the value (in the same atomic step) with <var class="Arg">new</var>. It returns true if the replacement took place, false otherwise.</p>
170
171 <p>The primary use of <code class="func">COMPARE_AND_SWAP</code> is to implement certain concurrency primitives; most programmers will not need to use it.</p>
172
173 <p><a id="X823767367D6CF4FD" name="X823767367D6CF4FD"></a></p>
174
175 <h4>5.2 <span class="Heading">Atomic records and component objects</span></h4>
176
177 <p>Atomic records are atomic counterparts to plain records. They support assignment to individual record fields, and conversion to and from plain records.</p>
178
179 <p>Assignment semantics can be specified on a per-record basis if the assigned record field is already populated, allowing either an overwrite, keeping the existing value, or raising an error.</p>
180
181 <p>It is not possible to unbind atomic record elements.</p>
182
183 <p>Like plain records, atomic records can be converted to component objects using <code class="code">Objectify</code>.</p>
184
185 <p><a id="X87EA343179BE4BFE" name="X87EA343179BE4BFE"></a></p>
186
187 <h5>5.2-1 AtomicRecord</h5>
188
189 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; AtomicRecord</code>( <var class="Arg">capacity</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
190 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; AtomicRecord</code>( <var class="Arg">record</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
191 <p><code class="func">AtomicRecord</code> is used to create a new atomic record. Its single optional argument is either a positive integer, denoting the intended capacity (i.e., number of elements to be held) of the record, in which case a new empty atomic record with that initial capacity will be created. Alternatively, the caller can provide a plain record with which to initially populate the atomic record.</p>
192
193
194 <div class="example"><pre>
195 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">r := AtomicRecord(rec( x := 2 ));</span>
196 &lt;atomic record 1/2 full&gt;
197 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">r.y := 3;</span>
198 3
199 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">TaskResult(RunTask(function() return r.x + r.y; end));</span>
200 5
201 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">[ r.x, r.y ];</span>
202 [ 2, 3 ]
203 </pre></div>
204
205 <p>Any atomic record can later grow beyond its initial capacity. There is no limit to the number of elements it can hold other than available memory.</p>
206
207 <p><a id="X81E6B21C7A1AC7FE" name="X81E6B21C7A1AC7FE"></a></p>
208
209 <h5>5.2-2 FromAtomicRecord</h5>
210
211 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; FromAtomicRecord</code>( <var class="Arg">record</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
212 <p><code class="func">FromAtomicRecord</code> returns a plain record copy of the atomic record <var class="Arg">record</var>. This copy is shallow; elements of <var class="Arg">record</var> will not also be copied.</p>
213
214
215 <div class="example"><pre>
216 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">r := AtomicRecord();;</span>
217 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">r.x := 1;; r.y := 2;; r.z := 3;;</span>
218 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">FromAtomicRecord(r);</span>
219 rec( x := 1, y := 2, z := 3 )
220 </pre></div>
221
222 <p><a id="X78EDB1DB79473F53" name="X78EDB1DB79473F53"></a></p>
223
224 <h4>5.3 <span class="Heading">Replacement policy functions</span></h4>
225
226 <p>There are three functions that set the replacement policy of an atomic object. All three can also be used with plain lists and records, in which case an atomic version of the list or record is first created. This allows programmers to elide <code class="func">AtomicList</code> (<a href="chap5_mj.html#X7F1353B58414D4C3"><span class="RefLink">5.1-1</span></a>) and <code class="func">AtomicRecord</code> (<a href="chap5_mj.html#X87EA343179BE4BFE"><span class="RefLink">5.2-1</span></a>) calls when the next step is to change their policy.</p>
227
228 <p><a id="X7B76F2AD818492BC" name="X7B76F2AD818492BC"></a></p>
229
230 <h5>5.3-1 MakeWriteOnceAtomic</h5>
231
232 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; MakeWriteOnceAtomic</code>( <var class="Arg">obj</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
233 <p><code class="func">MakeWriteOnceAtomic</code> takes a list, record, atomic list, atomic record, atomic positional object, or atomic component object as its argument. If the argument is a non-atomic list or record, then the function first creates an atomic copy of the argument. The function then changes the replacement policy of the object to write-once: if an element of the object is already bound, then further attempts to assign to it will be ignored.</p>
234
235 <p><a id="X806C3F0681B847A0" name="X806C3F0681B847A0"></a></p>
236
237 <h5>5.3-2 MakeStrictWriteOnceAtomic</h5>
238
239 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; MakeStrictWriteOnceAtomic</code>( <var class="Arg">obj</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
240 <p><code class="func">MakeStrictWriteOnceAtomic</code> works like <code class="func">MakeWriteOnceAtomic</code> (<a href="chap5_mj.html#X7B76F2AD818492BC"><span class="RefLink">5.3-1</span></a>), except that the replacement policy is being changed to being strict write-once: if an element is already bound, then further attempts to assign to it will raise an error.</p>
241
242 <p><a id="X7E7591EC780F3988" name="X7E7591EC780F3988"></a></p>
243
244 <h5>5.3-3 MakeReadWriteAtomic</h5>
245
246 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; MakeReadWriteAtomic</code>( <var class="Arg">obj</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
247 <p><code class="func">MakeReadWriteAtomic</code> is the inverse of <code class="func">MakeWriteOnceAtomic</code> (<a href="chap5_mj.html#X7B76F2AD818492BC"><span class="RefLink">5.3-1</span></a>) and <code class="func">MakeStrictWriteOnceAtomic</code> (<a href="chap5_mj.html#X806C3F0681B847A0"><span class="RefLink">5.3-2</span></a>) in that the replacement policy is being changed to being rewritable: Elements can be replaced even if they are already bound.</p>
248
249 <p><a id="X7DCE3B4D7D26407F" name="X7DCE3B4D7D26407F"></a></p>
250
251 <h4>5.4 <span class="Heading">Thread-local records</span></h4>
252
253 <p>Thread-local records allow an easy way to have a separate copy of a record for each individual thread that is accessed by the same name in each thread.</p>
254
255
256 <div class="example"><pre>
257 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">r := ThreadLocalRecord();; # create new thread-local record</span>
258 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">r.x := 99;;</span>
259 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">WaitThread( CreateThread( function()</span>
260 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> r.x := 100;</span>
261 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> Display(r.x);</span>
262 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> end ) );</span>
263 100
264 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">r.x;</span>
265 99
266 </pre></div>
267
268 <p>As can be seen above, even though <code class="code">r.x</code> is overwritten in the second thread, it does not affect the value of <code class="code">r.x| in the first thread</code></p>
269
270 <p><a id="X7BE7D67586FC91E6" name="X7BE7D67586FC91E6"></a></p>
271
272 <h5>5.4-1 ThreadLocalRecord</h5>
273
274 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; ThreadLocalRecord</code>( [<var class="Arg">defaults</var>[, <var class="Arg">constructors</var>]] )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
275 <p><code class="func">ThreadLocalRecord</code> creates a new thread-local record. It accepts up to two initial arguments. The <var class="Arg">defaults</var> argument is a record of default values with which each thread-local copy is initially populated (this happens on demand, so values are not actually read until needed). The second argument is a record of constructors; parameterless functions that return an initial value for the respective element. Constructors are evaluated only once per thread and only if the respective element is accessed without having previously been assigned a value.</p>
276
277
278 <div class="example"><pre>
279 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">r := ThreadLocalRecord( rec(x := 99),</span>
280 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> rec(y := function() return 101; end));;</span>
281 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">r.x;</span>
282 99
283 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">r.y;</span>
284 101
285 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">TaskResult(RunTask(function() return r.x; end));</span>
286 99
287 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">TaskResult(RunTask(function() return r.y; end));</span>
288 101
289 </pre></div>
290
291 <p><a id="X788AF1CC8111798C" name="X788AF1CC8111798C"></a></p>
292
293 <h5>5.4-2 SetTLDefault</h5>
294
295 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; SetTLDefault</code>( <var class="Arg">record</var>, <var class="Arg">name</var>, <var class="Arg">value</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
296 <p><code class="func">SetTLDefault</code> can be used to set the default value of a record field after its creation. Here, <var class="Arg">record</var> is a thread-local record, <var class="Arg">name</var> is the string of the field name, and <var class="Arg">value</var> is the value.</p>
297
298
299 <div class="example"><pre>
300 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">r := ThreadLocalRecord();;</span>
301 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">SetTLDefault(r, "x", 314);</span>
302 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">r.x;</span>
303 314
304 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">TaskResult(RunTask(function() return r.x; end));</span>
305 314
306 </pre></div>
307
308 <p><a id="X853A898384888B85" name="X853A898384888B85"></a></p>
309
310 <h5>5.4-3 SetTLConstructor</h5>
311
312 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; SetTLConstructor</code>( <var class="Arg">record</var>, <var class="Arg">name</var>, <var class="Arg">func</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
313 <p><code class="func">SetTLConstructor</code> can be used to set the constructor of a thread-local record field after its creation, similar to <code class="func">SetTLDefault</code> (<a href="chap5_mj.html#X788AF1CC8111798C"><span class="RefLink">5.4-2</span></a>).</p>
314
315
316 <div class="example"><pre>
317 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">r := ThreadLocalRecord();;</span>
318 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">SetTLConstructor(r, "x", function() return 2718; end);</span>
319 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">r.x;</span>
320 2718
321 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">TaskResult(RunTask(function() return r.x; end));</span>
322 2718
323 </pre></div>
324
325
326 <div class="chlinkprevnextbot">&nbsp;<a href="chap0_mj.html">[Top of Book]</a>&nbsp; <a href="chap0_mj.html#contents">[Contents]</a>&nbsp; &nbsp;<a href="chap4_mj.html">[Previous Chapter]</a>&nbsp; &nbsp;<a href="chap6_mj.html">[Next Chapter]</a>&nbsp; </div>
327
328
329 <div class="chlinkbot"><span class="chlink1">Goto Chapter: </span><a href="chap0_mj.html">Top</a> <a href="chap1_mj.html">1</a> <a href="chap2_mj.html">2</a> <a href="chap3_mj.html">3</a> <a href="chap4_mj.html">4</a> <a href="chap5_mj.html">5</a> <a href="chap6_mj.html">6</a> <a href="chap7_mj.html">7</a> <a href="chap8_mj.html">8</a> <a href="chap9_mj.html">9</a> <a href="chap10_mj.html">10</a> <a href="chap11_mj.html">11</a> <a href="chapInd_mj.html">Ind</a> </div>
330
331 <hr />
332 <p class="foot">generated by <a href="https://www.math.rwth-aachen.de/~Frank.Luebeck/GAPDoc">GAPDoc2HTML</a></p>
333 </body>
334 </html>